2个对象互相实例化时的异常

时间:2022-01-16 11:58:25

So I'm creating a simple text based game but I came up with an exception/error along the way. I have 3 classes one is a class called Main, one is a class called Mage, and one is a class called Warrior.

所以我正在创建一个简单的基于文本的游戏,但我在此过程中想出了异常/错误。我有3个类,一个是名为Main的类,一个是名为Mage的类,另一个是名为Warrior的类。

So in a game when a player uses an abbility it reduces the health points of the other player by a certant amount.

因此,在玩家使用可行性的游戏中,它会降低其他玩家的健康点数量。

So when an abbility is used by player1 for example in this case Fireball it calls the method reduceHP in Warrior class which reduces the HP of player2 by 44. The method reduceHP takes the number that is passed in and substracts it from the health points of the player.

因此,当玩家1使用abbility时,例如在这种情况下,Fireball会调用Warrior类中的reduceHP方法,将player2的HP减少44.方法reduceHP获取传入的数字并将其从健康点中减去播放器。

Warrior player2 = new Warrior();

public void FireBall(){

player2.reduceHP(44);

}

The same thing happens in Mage class. When the player1 uses a Warrior abbility it reduces the HP of player2 by 65.

同样的事情发生在Mage类中。当player1使用Warrior abbility时,它将player2的HP减少65。

Mage player1 = new Mage();

public void Stab(){

player1.reduceHP(65);


}

Now the problem is when I call these abbility/abbilities (stab and fireball) in the main class i get the following error

现在的问题是当我在主类中调用这些abbility / abbilities(stab和fireball)时我得到以下错误

at Warrior.<init>(Warrior.java:4)
at Mage.<init>(Mage.java:4)
at Warrior.<init>(Warrior.java:4)
at Mage.<init>(Mage.java:4)
at Warrior.<init>(Warrior.java:4)
at Mage.<init>(Mage.java:4)
at Warrior.<init>(Warrior.java:4)
at Mage.<init>(Mage.java:4)
at Warrior.<init>(Warrior.java:4)
at Mage.<init>(Mage.java:4)
at Warrior.<init>(Warrior.java:4)
at Mage.<init>(Mage.java:4)
at Warrior.<init>(Warrior.java:4)

ETC... it keeps on going

I'm wondering why does this happen ? If there is another way of doing this please let me know since I'm quite new to java. Thanks for all the help it is much appreciated and I hope this is not a really dumb question to ask on here.

我想知道为什么会这样?如果还有另一种方法,请告诉我,因为我对java很新。非常感谢所有的帮助,我希望这不是一个非常愚蠢的问题。

2 个解决方案

#1


0  

You should instantiate the players in your Main class and then pass references to them by setter. This way you will have one instance of each object and they will have references to each other.

您应该在Main类中实例化玩家,然后通过setter传递对它们的引用。这样,您将拥有每个对象的一个​​实例,并且它们将相互引用。

#2


1  

The two fields player1 and player2 are the issue. I think you want to have exactly two objects, one for Player 1 and one for Player 2. Instead, you are creating an infinite number of objects inside the (implicit) constructors.

两个字段player1和player2是问题。我想你想要有两个对象,一个用于Player 1,一个用于Player 2.相反,你在(隐式)构造函数中创建了无数个对象。

I guess you're a beginner, so I will not discuss the details here. To solve the issue, you might want to create a third class (maybe named Game?) which knows about the two players.

我想你是初学者,所以我不会在这里讨论细节。要解决这个问题,您可能想要创建一个知道这两个玩家的第三个类(可能名为Game?)。

#1


0  

You should instantiate the players in your Main class and then pass references to them by setter. This way you will have one instance of each object and they will have references to each other.

您应该在Main类中实例化玩家,然后通过setter传递对它们的引用。这样,您将拥有每个对象的一个​​实例,并且它们将相互引用。

#2


1  

The two fields player1 and player2 are the issue. I think you want to have exactly two objects, one for Player 1 and one for Player 2. Instead, you are creating an infinite number of objects inside the (implicit) constructors.

两个字段player1和player2是问题。我想你想要有两个对象,一个用于Player 1,一个用于Player 2.相反,你在(隐式)构造函数中创建了无数个对象。

I guess you're a beginner, so I will not discuss the details here. To solve the issue, you might want to create a third class (maybe named Game?) which knows about the two players.

我想你是初学者,所以我不会在这里讨论细节。要解决这个问题,您可能想要创建一个知道这两个玩家的第三个类(可能名为Game?)。