简体   繁体   English

不同类中的两个相同变量

[英]Two of the same variables in different classes

I could only find ac# link for this and couldn't understand it so here goes.. 我只能为此找到ac#link并且无法理解它所以这里...

I have a Player and a Target class in my program, and I want to be able to make like this: 我的程序中有一个Player和一个Target类,我希望能够这样做:

public Player(int difficulty)
{
    if(difficulty == 1)
        health = hArray[0];
    else
        health = hArray[1];
}

where hArray is my array of possible health values. 其中hArray是我可能的健康值数组。 This works, but I also deal with playerHealth inside of my Target class just for ease of use because I'm new to java sort of and didn't know how else to do it, so that has something like this: 这有效,但我也只是为了易于使用而处理我的Target类中的playerHealth,因为我是java的新手并且不知道怎么做,所以它有这样的东西:

private int playerHealth;
public int getPlayerHealth()
{
    return playerHealth;
}


public void setPlayerHealth(int playerHealth){
    this.playerHealth = playerHealth;
}

But I don't know how to call this anymore because originally I had it set for Player to always start with 100 health. 但是我不知道怎么称呼这个,因为最初我把它设置为Player总是从100健康开始。 I changed it because I need to set the option of health value in Player as a requirement and now when I call this in main (psuedo-ish): 我更改了它,因为我需要在Player中设置健康值选项作为要求,现在当我在main(psuedo-ish)中调用它时:

Your starting health: " + tgt.getPlayerHealth()

It just says 0 because the health was defined in Player. 它只是说0,因为健康是在播放器中定义的。 So I know why it isn't working but I'm not sure how to fix it, or if I can fix it with my current layout. 所以我知道为什么它不起作用,但我不知道如何解决它,或者我是否可以用我当前的布局修复它。

Here are my declarations/instanstiations (is this a word?): 这是我的声明/实例(这是一个字吗?):

    Target tgt = new Target(dif);
    Player p1 = new Player(dif);

Based off of: 基于:

System.out.println("\nEnter a difficulty 1-2 (entering an integer "
            + "other than 1 or 2 will default to 2):");
dif = scn.nextInt();

any help is appreciated 任何帮助表示赞赏

Given that you properly modeled your two classes (which I doubt a bit): if your Target needs to know about the player health, it should have a reference to a Player instance and not hold a copy of one of the private members of Player . 既然你正确建模的两班(我怀疑了一下):如果你的Target需要了解球员的健康,它应该有一个参考Player 的实例 ,而不是拖延的私有成员之一的副本Player Then you'll access the player health via that instance: 然后,您将通过该实例访问玩家健康状况:

target.getPlayer().getHealth();
target.getPlayer().setHealth(50);

But once a Target has a reference to one Player , the names of the classes (if not the design in its entirety) seem quite inadequate. 但是一旦Target有一个Player的引用,类的名称(如果不是整个设计)似乎是不够的。

After knowing that Target is suppose to be an entity of some kind that fights your Player and makes them lose their health, hence the need for target to know about player health, then I think you would want to have an operation belonging to Player that can fight Target s (just pass Target to Player , or the other way around). 在知道Target被认为是某种类型的实体与你的Player战斗并使他们失去健康,因此需要目标知道玩家的健康状况后,我想你会想要一个属于Player的操作可以战斗Target s(只是将Target传递给Player ,或者相反)。 Something along the lines of the following would most likely work: 以下内容最有可能奏效:

public interface Entity
{
    public int getStrength();
    public int damageThatCanBeDoneToAnotherEntity(Entity someOtherEntity);
    public int getHealth();
    public void fightAnotherEntity(Entity someOtherEntity);
}

public class WeakMonster implements Entity...
public class StrongMonster implements Entity...
public class Player implements Entity...

This is your homework assignment so I'll let you fill in the gaps. 这是你的家庭作业,所以我会让你填补空白。

rather than having an array like you do with the hArray, you should create a function in Player like 而不是像使用hArray那样使用数组,你应该在Player中创建一个函数

public static int getPlayerInitialHealth(int difficulty)

then you can use that function in both of your classes (import into the second class) 然后你可以在你的两个类中使用该函数(导入到第二个类)

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

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