我有方法drawPoliticCard()
,我需要在另一个类applyBonus()
的方法中使用,我可以使用静态属性轻松完成,但问题是我应该能够运行两个游戏的实例独立于同一时间,因此如果使用静态,两个实例将共享同一副牌。这就是为什么我想知道是否有办法在不使用静态属性的情况下完成它。我将省略属性的getter和setter以使其更简洁。
public class PoliticCard {
private static ArrayList<Color> politicCards;
public void setDeck(){
setPoliticCards(new ArrayList<Color>());
getPoliticCards().add(Color.BLACK);
getPoliticCards().add(Color.PURPLE);
getPoliticCards().add(Color.BLUE);
}
public static void drawPoliticCard(Player player){
player.getPoliticCards().add(getPoliticCards().get(0));
getPoliticCards().remove(0);
}
}
public class Bonus {
protected int number;//The number of politic cards added in BonusPoliticCard
}
public class BonusPoliticCard extends Bonus {
public BonusPoliticCard(int number) {
this.number=number;
}
public void applyBonus(Player player){
int i=0;
while(i<number){
PoliticCard.drawPoliticCard(player);
i++;
}
}
}
我知道要从另一个函数调用一个方法,你应该创建一个新的类实例,但是如果我在测试时得到一个nullpointerexception
。
public class BonusPoliticCard extends Bonus {
public BonusPoliticCard(int number) {
this.number=number;
}
public void applyBonus(Player player){
int i=0;
while(i<number){
PoliticCard politicCard = new PoliticCard();
politicCard.drawPoliticCard(player);
i++;
}
}
}
public class BonusPoliticCardTest {
@Test
public void testBonusPoliticCard() {//This ran fine when it was static
Bonus bonus = new BonusPoliticCard(3);
Player player = new Player(1);
PoliticCard politicCard = new PoliticCard();
politicCard.setDeck();
bonus.applyBonus(player);
assertNotNull(player.getPoliticCards().get(2));
}
}
答案 0 :(得分:0)
您可以在类Bonus的构造函数中创建类PoliticCard的实例。因此,您将能够一起使用它们并创建多个游戏实例:
public class BonusPoliticCard extends Bonus {
PoliticCard p;
public BonusPoliticCard(int number) {
this.number=number;
p = new PoliticCard();
}
public void applyBonus(Player player){
int i=0;
while(i<number){
p.drawPoliticCard(player);
i++;
}
}
}
答案 1 :(得分:0)
我对您的代码设计有点困惑,我有以下问题 -
BonusPoliticCard extends PoliticCard
更有意义。PoliticCard HAS-A BONUS
。所以在我看来你应该再考虑一下你的设计。我的答案仅基于提供的代码。
答案 2 :(得分:0)
由于问题中没有显示Player类,因此有两种情况可以抛出NullPointerException:
(1)在BonusPoliticCardTest中创建的PoliticCard实例与您在BonusPoliticCard类中创建的实例不同。因此,您在BonusPoliticCardTest中通过politicCard.setDeck()设置的politicCards arrayList永远不会在BonusPoliticCard类中创建的PoliticCard实例中可用。另请注意,PoliticCard实例的politicCards arrayList都是在setDeck()内创建和设置的。因此,在对bonus.applyBonus(玩家)的调用期间; politicCards arrayList引用指向null,因此将在getPoliticCards()的后续行抛出NullPointerException。get(0):
player.getPoliticCards().add(getPoliticCards().get(0)); // inside drawPoliticCard(Player player) method
(2)Player类应该在其构造函数中创建ArrayList politicCards,否则我们将在以下行为player.getPoliticCards()获取NullPointerException。add(...):
player.getPoliticCards().add(getPoliticCards().get(0)); // inside drawPoliticCard(Player player) method
以下是正确的代码示例:
PoliticCard.java
public class PoliticCard {
private ArrayList<Color> politicCards;
/**
* @return the politicCards
*/
public ArrayList<Color> getPoliticCards() {
return politicCards;
}
/**
* @param politicCards the politicCards to set
*/
public void setPoliticCards(ArrayList<Color> politicCards) {
this.politicCards = politicCards;
}
public void setDeck() {
setPoliticCards(new ArrayList<Color>());
getPoliticCards().add(Color.BLACK);
getPoliticCards().add(Color.PURPLE);
getPoliticCards().add(Color.BLUE);
}
public void drawPoliticCard(Player player) {
player.getPoliticCards().add(getPoliticCards().get(0));
getPoliticCards().remove(0);
}
}
Bonus.java:
public class Bonus {
protected int number; //The number of politic cards added in BonusPoliticCard
}
BonusPoliticCard.java
public class BonusPoliticCard extends Bonus {
PoliticCard politicCard;
public BonusPoliticCard(int number, PoliticCard politicCard) {
this.number=number;
this.politicCard = politicCard;
}
public void applyBonus(Player player){
int i=0;
while(i<number){
politicCard.drawPoliticCard(player);
i++;
}
}
}
Player.java
public class Player {
private ArrayList<Color> politicCards;
int num;
public Player(int num) {
this.num = num;
politicCards = new ArrayList<Color>();
}
/**
* @return the politicCards
*/
public ArrayList<Color> getPoliticCards() {
return politicCards;
}
/**
* @param politicCards the politicCards to set
*/
public void setPoliticCards(ArrayList<Color> politicCards) {
this.politicCards = politicCards;
}
}
BonusPoliticCardTest.java
public class BonusPoliticCardTest {
@Test
public void test() {
PoliticCard politicCard = new PoliticCard();
politicCard.setDeck();
Bonus bonus = new BonusPoliticCard(3, politicCard);
Player player = new Player(1);
((BonusPoliticCard) bonus).applyBonus(player);
assertNotNull(player.getPoliticCards().get(2));
}
}