Je suis nouveau à Java. La question peut donc sembler naïve ... Mais pourriez-vous m'aider?Comment appeler une méthode publique WITHIN dans un attribut privé?
Disons, par exemple, j'ai une classe comme suit:
public class Human {
private float height;
private float weight;
private float hairLength;
private HairState hairTooLong = new HairState() {
@Override
public void cut(Human paramHuman) {
Human.this.decreaseHairLength();
}
@Override
public void notCut(Human paramHuman) {
Human.this.increaseHairLength();
}
};
public float increaseHairLength() {
hairLength += 10;
}
public float decreaseHairLength() {
hairLength -= 10;
}
private static abstract interface HairState {
public abstract void cut(Human paramHuman);
public abstract void notCut(Human paramHuman);
}
}
Puis j'ai une autre classe comme suit:
public class Demo {
public static void main(String[] args) {
Human human1 = new Huamn();
Human.HairState.cut(human1);
}
}
La déclaration
Human.HairState.cut(human1);
est invalide ..
J'ai l'intention d'appeler la fonction publiquecut()
, qui appartient à hairTooLong
privé attribut.
Comment dois-je faire?
Vous ne pouvez pas. Il ne peut être appelé qu'à partir de 'Human' ... – MadProgrammer