简体   繁体   English

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

[英]Using a object specific variable in another class

I've been looking across all the other questions but i cant seem to find an answer to my problem: I need to use a non-static field (which is separate for each object i create) and I need to access that variable in another class. 我一直在寻找所有其他问题,但我似乎找不到问题的答案:我需要使用一个非静态字段(对于我创建的每个对象都是独立的),并且需要在另一个字段中访问该变量类。 Piece of the code: (Enemytank.java) 代码片段:(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);
    }
}

And the class in which i need the variable enemyStage : (Playertank.java) 和我需要在其中变量variableStage的 :(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();
        }
    }

The value I now get from enemyTank1.getEnemyStage() is 0, because i believe i make a new object with Enemytank enemyTank1 = new Enemytank(mygame); 我现在从仇敌坦克1.getEnemyStage()得到的值是0,因为我相信我用敌人坦克制造了一个新物体敌人坦克1 =新敌人坦克(mygame); . How can I refer to the existing object, and get that specific value? 如何引用现有对象并获得特定值? enemyStage is used to check if the monster is dead or almost dead. evilStage用于检查怪物是否死亡或几乎死亡。

Thx in advance :) Flame 提前Thx :)火焰

You need to leverage polymorphism. 您需要利用多态性。 Declare the method getEnemyStage in the GameItem class (or is it interface?) and then you'll be able to ask collidedItem.getEnemyStage() and get the data from exactly that object. GameItem类中声明方法getEnemyStage (或者是接口?),然后您可以询问collidedItem.getEnemyStage()并从该对象中获取数据。 Alternatively, downcast the collidedItem into EnemyTank and call the method: ((EnemyTank)collidedItem).getEnemyStage() 可替代地,所述向下转换 collidedItemEnemyTank并调用该方法: ((EnemyTank)collidedItem).getEnemyStage()

Also, make enemyStage private , no need to be public as it will be accessed indirectly, by calling the getter method. 另外,将enemyStage private ,无需public ,因为可以通过调用getter方法间接访问它。

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

You pass a refrence to the object when you call collisionOccured. 当您调用冲突对象时,您将给对象传递一个引用。 so, change collisionOccured to this: 因此,将冲突更改为:

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();
        }
    }

And, when you call collisionOccured from within EnemyTank class you like this: 而且,当您从EnemyTank类中调用冲撞事故时,您会像这样:

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

With this answer, I assume that you call collisonOccured from within the EnemyTank class as you did not post where that call is beeing made. 有了这个答案,我假设您是从EnemyTank类中调用collisonOccured的,因为您没有在该调用的位置发帖。 If not, you need a refrence to a object of EnemyTank when you call collisionOccured (and therefore change this in the call to refrence to the correct object) 如果没有,你需要一个refrence到EnemyTank的对象,当你调用collisionOccured(并因此改变this在调用refrence到正确的对象)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM