Access an internal Java class

1

Why is it not a problem to access an internal Java class in two stages:

Clause.Litteral l = (Clause.Litteral)copieResolvante.listeLitteraux.get(0);

if(l.predicat.equals("write")){
                    Clause.Litteral.Symbole s = (Clause.Litteral.Symbole)l.get(0);
                    System.out.println("VOILA CE QU'IL Y AVAIT AU CHALET: " + s + "\n\n\n");

... and is it a problem when we try to do it in one time?

while((Clause.Litteral)copieResolvante.listeLitteraux.get(cptrLit).predicat.equals("write")){

This is the architecture of our program:

    
asked by ThePassenger 25.04.2016 в 20:42
source

1 answer

3

Apparently the problem is here:

(Clause.Litteral)copieResolvante.listeLitteraux.get(cptrLit).predicat
//..........................................................| no se sabe que predicat es parte de lo retornado por listeLitteraux.get(cptrLit)

You must use parentheses to group the variable or value on which you need to cast, and then call the corresponding attribute or method, like this:

((Clause.Litteral)copieResolvante.listeLitteraux.get(cptrLit)).predicat

Thus, your code would look (put in multiple lines to understand its meaning):

while(
    (
        //castear a Clause.Litteral el resultado de
        //copieResolvante.listeLitteraux.get(cptrLit)
        (Clause.Litteral)copieResolvante.listeLitteraux.get(cptrLit)
    ).predicat //sobre ese resultado, acceder al atributo predicat
    .equals("write")) { //sobre el atributo predicat. acceder al método equals
}

However, as a good practice for code readability, since according to the code that you are going to use then other attributes / methods of the result of (Clause.Litteral)copieResolvante.listeLitteraux.get(cptrLit) , it would be better to store this result in a variable.

    
answered by 25.04.2016 / 21:17
source