[英]Java Inheritance : 2nd instance calls 1st instance method
I have a very strange problem with a java class in my android application. 我的Android应用程序中的Java类有一个非常奇怪的问题。
I have some sub classes which extend my abstract class GameDialog 我有一些子类扩展了我的抽象类GameDialog
GameDialog class GameDialog类
public abstract class GameDialog extends Sprite
{
private static boolean gd_visible = false;
protected GameDialog(GameScene scene, Camera camera, VertexBufferObjectManager pSpriteVertexBufferObject){
...
}
public boolean display(){
if(!GameDialog.gd_visible) {
...
}
}
protected boolean hide(){
if(GameDialog.gd_visible){
...
}
}
}
PauseDialog class PauseDialog类
public class PauseDialog extends GameDialog {
public PauseDialog(GameScene scene, Camera camera, VertexBufferObjectManager pSpriteVertexBufferObject) {
super(scene, camera, pSpriteVertexBufferObject);
...
final ButtonSprite play_button = new ButtonSprite(...);
play_button.setOnClickListener(setPlayButtonListener());
}
private OnClickListener setPlayButtonListener() {
return new OnClickListener() {
@Override
public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
hide();
}
};
}
}
Each time I want to display a dialog, I write this line : new PauseDialog(GameScene.this, camera, vbom).display(); 每当我想显示一个对话框时,我都写以下行:new PauseDialog(GameScene.this,camera,vbom).display();
The first time, it works well : the dialog is displayed, user make a choice and it's hidden. 第一次,它运行良好:显示对话框,用户做出选择,并且对话框被隐藏。 But the 2nd time, the dialog is not hidden (after user's choice).
但是第二次,对话框没有被隐藏(在用户选择之后)。
I used the debugger to see what's going on, and the conclusion is : 我使用调试器来查看发生了什么,结论是:
If some one can explain me what it is doing that ... Thank you. 如果有人可以解释一下我在做什么呢……谢谢。
It is because gd_visible
is static
. 这是因为
gd_visible
是static
。 Delete the static keyword and it should work. 删除static关键字,它应该可以工作。 Static fields doesn't belong to the instances but they belong to the class.
静态字段不属于实例,但它们属于类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.