我有下面的代码,尽管类和成员方法是公共(public)的,但我无法在 methodLMF 中引用 methodHF 而不是 methodBF。我尝试了以下方法:
LMF.this.xxxx //but the methods do not show up
请告诉我如何修复它。
代码:
class LMF {
LMF() {}
public methodLMF() { } // should return methodHF+methodBF
//class HF
class HF {
HF() {}
public methodHF(int x) {x++}
}
//class BF
class BF {
BF() {}
public methodBF(int x) {x++}
}
}
最佳答案
您需要创建 HF 和 BF 对象才能访问那里的方法。
class LMF {
LMF() {
}
public int methodLMF(int x) {
return new HF().methodHF(x) + new BF().methodBF(x);
} // should return methodHF+methodBF
// class HF
class HF {
HF() {
}
public int methodHF(int x) {
return x++;
}
}
// class BF
class BF {
BF() {
}
public int methodBF(int x) {
return x++;
}
}
public static void main(String[] args) {
System.out.println(new LMF().methodLMF(1));
}
}
关于java - 从外部类引用内部类中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31786454/