Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1446 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  [JAVA] Inner class et accès aux membres protected --> compiler bug ?

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[JAVA] Inner class et accès aux membres protected --> compiler bug ?

n°772741
noldor
Rockn'roll
Posté le 22-06-2004 à 14:24:00  profilanswer
 

Un exemple valant mieux qu'une explication alambiquée, voilà mon problème (exemple bateau) :
 
J'ai une classe fruit.Fruit

Code :
  1. package fruit;
  2. public class Fruit {
  3.         protected String name;
  4. }


 
et une classe orange.Orange :
 

Code :
  1. package orange;
  2. import fruit.Fruit;
  3. public class Orange extends Fruit {
  4.         public Orange() {
  5.                 this.name = "orange";
  6.         }
  7.         public void print() {
  8.                 new InnerClass().print();
  9.         }
  10.         private class InnerClass {
  11.                 void print() {
  12.                         System.out.println(name);
  13.                 }
  14.         }
  15.         public static void main(String[] args) {
  16.                 Orange o = new Orange();
  17.                  o.print();
  18.         }
  19. }


 
Lorsque je compile Orange avec le JDK 1.2.2, j'ai le message suivant :

Citation :

orange/Orange.java:17: Variable name in class fruit.Fruit not accessible from inner class orange.Orange. InnerClass.
                        System.out.println(name);
                                           ^
1 error


 
Avec une JDK 1.1, ou une >=1.3 ça passe
s'agit il d'un bug du compilateur d'après vous ?


Message édité par noldor le 22-06-2004 à 14:26:15

---------------
http://runnerstats.net
mood
Publicité
Posté le 22-06-2004 à 14:24:00  profilanswer
 

n°772769
benou
Posté le 22-06-2004 à 14:40:43  profilanswer
 

pour moi ca devrait marcher ...


---------------
ma vie, mon oeuvre - HomePlayer
n°772771
noldor
Rockn'roll
Posté le 22-06-2004 à 14:42:26  profilanswer
 

En parcourant la bug parade de Sun, il s'agit bien d'un bug du JDK 1.2 :
http://bugs.sun.com/bugdatabase/vi [...] id=4116802 pour ceux qui veulent plus d'infos


---------------
http://runnerstats.net
n°772778
Taz
bisounours-codeur
Posté le 22-06-2004 à 14:45:45  profilanswer
 

les inner classe sont liées à une instance en Java ?

n°772782
nraynaud
lol
Posté le 22-06-2004 à 14:47:51  profilanswer
 

oui, biensûr, c'est ce qui les différencie des classes simplement "nested" (qui sont déclarées pareil, mais avec le mot clef "static" ) on accède à l'instance englobante par ClasseEnglobante.this .


---------------
trainoo.com, c'est fini
n°772791
Taz
bisounours-codeur
Posté le 22-06-2004 à 14:52:18  profilanswer
 

ok, merci.
 
la principale fonction de ces classes, c'est de jouer le rôle de proxy/propriété ?

n°772821
nraynaud
lol
Posté le 22-06-2004 à 15:15:56  profilanswer
 

Taz a écrit :

ok, merci.
 
la principale fonction de ces classes, c'est de jouer le rôle de proxy/propriété ?

bof, je les utilise quand j'ai besoin d'une toute petite classe non-réutilisable (implémentation concrète), style les handlers dévènement. Comme il y a capture du contexte, je les utilise comme fermeture lexicale aussi.
 
petits exemples
en closure :

Code :
  1. public class URLDecoration extends Decoration {
  2.     public static Decoration.Factory getFactory(final String url) {
  3.         return new Decoration.Factory() {
  4.             public Decoration createDecoration(int start, int len, List frags) {
  5.                 return new URLDecoration(start, len, frags, url);
  6.             }
  7.         };
  8.     }
  9.     private String url;
  10.     /**
  11.      * @param start
  12.      */
  13.     protected URLDecoration(int start, int len, List frags, String url) {
  14.         super(start, len, frags);
  15.         this.url = url;
  16.     }


 
en petite classe non-réutilisable :  

Code :
  1. public class MessageCreator {
  2.     //TODO (nraynaud) : add filter installation/removal facility.
  3.     //TODO (nraynaud) : avoid using substring() during Stringbuffer use
  4.     /**
  5.      * the accumulating buffer
  6.      */
  7.     private StringBuffer buffer;
  8.     private final List stack = new ArrayList(2);
  9.     private List filters;
  10.     private class StackFrame {
  11.         /**
  12.          * the immediately preceding frame.
  13.          */
  14.         private StackFrame parent;
  15.         /**
  16.          * the factory of the decoration
  17.          */
  18.         public Decoration.Factory factory;
  19.         /**
  20.          * absolute start of the decoration from the begining of the buffer.
  21.          */
  22.         public int absoluteStart;
  23.         /**
  24.          * List of fragments collected so far
  25.          */
  26.         public List fragments = new LinkedList();
  27.         /**
  28.          * relative star of the last opened fragment, relative to the begining
  29.          * of the decoration
  30.          */
  31.         public int fragStart = 0;
  32.         public StackFrame(Decoration.Factory factory, StackFrame parent) {
  33.             this.factory = factory;
  34.             this.absoluteStart = buffer.length();
  35.             this.parent = parent;
  36.         }
  37.         /**
  38.          * @param fragStart
  39.          *            The fragStart to set.
  40.          */
  41.         private void moveFragStart(int val) {
  42.             this.fragStart += val;
  43.         }
  44.         /**
  45.          * consumes the last fragment form fragStart to the end of buff and add
  46.          * it to the fragments list. If fragment is empty, don't do anything.
  47.          *  
  48.          * @param buff
  49.          */
  50.         public void consumeFragment() {
  51.             consumeFragment(null);
  52.         }
  53.         /**
  54.          * consumes the last fragment, giving it the decoration into the
  55.          * constructor
  56.          *  
  57.          * @param decoration
  58.          */
  59.         public void consumeFragment(Decoration decoration) {
  60.             int start = absoluteStart + fragStart;
  61.             int len = buffer.length() - start;
  62.             if (len > 0) {
  63.                 fragments.add(new TextFragment(fragStart, len, decoration));
  64.                 moveFragStart(len);
  65.             }
  66.         }
  67.         /**
  68.          * transforms the current frame in decoration. Frame should not be used
  69.          * anymore after calling this function.
  70.          *  
  71.          * @return the created decoration, null if its length would have been
  72.          *         null.
  73.          */
  74.         public Decoration extractDecoration() {
  75.             int start;
  76.             if (parent != null)
  77.                 start = absoluteStart - parent.absoluteStart;
  78.             else
  79.                 start = absoluteStart;
  80.             int len = buffer.length() - absoluteStart;
  81.             if (len > 0)
  82.                 return factory.createDecoration(start, len, fragments);
  83.             else
  84.                 return null;
  85.         }
  86.     }
  87. ...


---------------
trainoo.com, c'est fini
n°772879
Taz
bisounours-codeur
Posté le 22-06-2004 à 15:58:43  profilanswer
 

ok, merci


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  [JAVA] Inner class et accès aux membres protected --> compiler bug ?

 

Sujets relatifs
[JAVA - DEBUTANT] Gérer la position d'un texte[JAVA] Pb recuperation evenement clavier
[java] Reseau : j'ai pas la meme chose avec java qu'en telnet...lister un repertoir en java
[Java] import d'informations d'une page Web[ACCESS]combobox accès au data dans les colonnes
Bp de compilation de classe java[.htaccess] Redirection accès page en fonction des groupes ?
[java 3D] java.lang.NoClassDefFoundError : sun/awt/DrawingSurface[java] performance d'une copie de fichier
Plus de sujets relatifs à : [JAVA] Inner class et accès aux membres protected --> compiler bug ?


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR