Java不使用静态变量

时间:2016-06-04 15:44:35

标签: java static instance

我有方法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));

    }

}

3 个答案:

答案 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)

我对您的代码设计有点困惑,我有以下问题 -

  1. 根据类的名称 - BonusPoliticCard,它应该扩展PoliticCard类。 BonusPoliticCard extends PoliticCard更有意义。
  2. 我们真的需要Bonus类才能保留BonusPoliticCard中添加的政治卡数。在我看来,如果需要Bonus类,那么我认为PoliticCard和Bonus之间应该有HAS-A关系。 PoliticCard HAS-A BONUS
  3. 我们通常应该避免使用静力学,除非确实需要它们。如果我们认为我们需要静力学,那么我们应该在需求和设计上一次又一次地思考,那么只有我们应该去静力学。对于实用程序类,我们可以进行静态分析。 我在这个here找到了一篇有用的文章。
  4. 所以在我看来你应该再考虑一下你的设计。我的答案仅基于提供的代码。

答案 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));
    }

}