在另一个类中使用特定于对象的变量

时间:2012-04-20 11:19:18

标签: java oop object instance non-static

我一直在查看所有其他问题,但我似乎无法找到我的问题的答案:我需要使用非静态字段(对于我创建的每个对象是独立的),我需要访问另一个类中的变量。一段代码:(Enemytank.java)

public class Enemytank extends MoveableGameItem implements IStepListener, IAlarmListener
{
    private Battlefield mygame;

    private Enemytank enemyTank1;   
    private static int enemyWaveSize = 1;
    private static int remainingKills = enemyWaveSize;
    private static double startupEnemyHealth = 100.00;
    private double enemyHealth = startupEnemyHealth;
    public int enemyStage = 0;              


    /**
     * Constructor.
     */
    public Enemytank(Battlefield mg)
    {
        mygame = mg;
        setImage("/images/enemytank.png", 27, 33);
        setPosition(25, 35);
        // snelheid 5, naar rechts
        setDirectionSpeed(0, 4);
        startMoving();
        mygame.addStepListener(this);
    }
}

我需要变量 enemyStage 的类:(Playertank.java)

public void collisionOccured(GameItem collidedItem)
    {
        Enemytank enemyTank1 = new Enemytank(mygame);
        System.out.println("pt= " + enemyTank1.getEnemyStage());
        if ((collidedItem instanceof Enemytank) && (playerShields > 0) && (enemyTank1.getEnemyStage() != 3))
        {
            this.playerShields--;
            mygame.setShieldsonDashboard(playerShields);

        }
        else if ((collidedItem instanceof Enemytank) && (playerShields <= 0) && (enemyTank1.getEnemyStage() != 3))
        {
            System.out.println("hit player!");
            this.playerLives--;
            mygame.setLivesonDashboard(playerLives);
        }
        if (collidedItem instanceof Shield)
        {
            this.playerShields++;
            mygame.setShieldsonDashboard(playerShields);
            mygame.deleteGameItem(collidedItem);
            this.playerUpgrades();
        }
    }

我现在从enemyTank1.getEnemyStage()得到的值是0,因为我相信我用 Enemytank enemyTank1 = new Enemytank(mygame); 创建一个新对象。如何引用现有对象并获取该特定值? enemyStage用于检查怪物是否死亡或几乎死亡。

提前thx :) 火焰

2 个答案:

答案 0 :(得分:0)

您需要利用多态性。在getEnemyStage类中声明方法GameItem(或者它是接口?)然后您将能够询问collidedItem.getEnemyStage()并从该对象获取数据。或者,向下转换 collidedItemEnemyTank并调用方法:((EnemyTank)collidedItem).getEnemyStage()

另外,通过调用getter方法,使enemyStage private成为public,因为它将被间接访问。

if (collidedItem instanceof Enemytank && playerShields > 0 && 
    ((Enemytank)collidedItem).getEnemyStage() != 3) {
  ....
}

答案 1 :(得分:0)

当您调用collisionOccured时,您会向对象传递参考。所以,更改collisionOccured到此:

public void collisionOccured(GameItem collidedItem, Enemytank enemyTank) //a change here
    {
        System.out.println("pt= " + enemyTank.getEnemyStage()); // now you can call enemyTank with the correct refrence
        if ((collidedItem instanceof Enemytank) && (playerShields > 0) && (enemyTank1.getEnemyStage() != 3))
        {
            this.playerShields--;
            mygame.setShieldsonDashboard(playerShields);

        }
        else if ((collidedItem instanceof Enemytank) && (playerShields <= 0) && (enemyTank.getEnemyStage() != 3))
        {
            System.out.println("hit player!");
            this.playerLives--;
            mygame.setLivesonDashboard(playerLives);
        }
        if (collidedItem instanceof Shield)
        {
            this.playerShields++;
            mygame.setShieldsonDashboard(playerShields);
            mygame.deleteGameItem(collidedItem);
            this.playerUpgrades();
        }
    }

而且,当您从EnemyTank类中调用collisionOccured时,您喜欢这样:

collisionOccured(collidedItem, this); //where this refrence to this object (which is a EnemyTank)

通过这个答案,我假设您在EnemyTank类中调用了collisonOccured,因为您没有发布该调用所在的位置。如果没有,当你调用collisionOccured时,你需要对EnemyTank的一个对象有所了解(因此在对正确对象的引用的调用中更改this