嵌套的内部类 ABar 和 BBar 是否可以访问主类的变量?例如:
public class Foo {
public ABar abar = new ABar();
public BBar bbar = new BBar();
public int someCounter = 0;
public class ABar {
public int i = 0;
public void someMethod(){
i++;
someCounter++;
}
}
public class BBar {
public void anotherMethod(){
bbar.someMethod();
someCounter++;
}
}
}
// then called using: //
Foo myFoo = new Foo();
myFoo.bbar.anotherMethod();
编辑
如果我先尝试的话,我输入的代码似乎会起作用;试图在不太具体的情况下寻求帮助。我实际上遇到问题的代码
由于错误“无法对非静态字段阶段进行静态引用”而失败
public class Engine {
public Stage stage = new Stage();
// ...
public class Renderer implements GLSurfaceView.Renderer {
// ...
@Override
public void onDrawFrame(GL10 gl) {
stage.alpha++;
}
}
public class Stage extends MovieClip {
public float alpha = 0f;
}