简体   繁体   English

Java继承:第2个实例调用第1个实例方法

[英]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 : 我使用调试器来查看发生了什么,结论是:

  • In the 2nd instance, it calls the hide() method of the first instance ! 在第二个实例中,它调用第一个实例的hide()方法!

If some one can explain me what it is doing that ... Thank you. 如果有人可以解释一下我在做什么呢……谢谢。

It is because gd_visible is static . 这是因为gd_visiblestatic 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.

相关问题 Java多线程第二线程等待第一 - Java multithreading 2nd thread waiting for 1st 在 Java 中的字符串中查找第 1 次和第 2 次出现的正则表达式 - Finding the 1st and 2nd occurrence of a regex in a string in Java 在 java 中将第一个数组值乘以 1,将第二个值乘以 3 - Multiply 1st array value with 1 and 2nd value with 3 in java 关于第一个init()和第二个init() - regarding 1st init() and 2nd init() 检查第一个 if 条件,然后检查第二个 - Checking 1st if condition and then 2nd 如何在 class 中将方法从第一种方法调用到第二种方法 - How to call method from 1st method to 2nd method in a class 如何通过从第1类调用第2类的方法来调用第3类的方法 - How to call a method of a 3rd class by calling the method of the 2nd class from the 1st class Spring JPA 实体 findBy<Parameter> 在第一次尝试中给出值并在第二次调用中给出 null 的方法? - Spring JPA entity findBy<Parameter> method giving values in 1st attempt and null in 2nd call? 如何通过单击第一个Java Windows应用程序(Jframe)中的菜单项启动第二个Jframe - How to start 2nd Jframe by click menu item in 1st java windows application (Jframe) 带有CardLayout的Java Swing GUI:返回第1张时,窗口未在第二张卡中关闭 - Java Swing GUI with CardLayout: Window not closing in 2nd Card when going back to 1st
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM