Java在不创建实例的情况下调用方法

时间:2016-06-02 16:52:13

标签: java arraylist instance

请原谅我,如果这个问题看起来对于普通的java程序员来说是愚蠢的,但我坚持这个问题。 我想从类getPoliticCards()中的类PoliticCard调用方法DrawCard(Player player)。起初我在static arraylist使用PoliticCard所以我没有问题,但我不得不改变它,因为我应该能够同时运行几个游戏。

public enum Color {
    BLACK, PURPLE
}
public class Player {
    private int id;
    private ArrayList<Color> politicCards;
    public Player(int id){
        this.setId(id);
        ArrayList<Color> array=new ArrayList<Color>();
        setPoliticCards(array);
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public ArrayList<Color> getPoliticCards() {
        return politicCards;
    }
    public void setPoliticCards(ArrayList<Color> politicCards) {
        this.politicCards = politicCards;
    }
}
public class PoliticCard {
    private ArrayList<Color> politicCards;
    public PoliticCard(){
        setPoliticCards(new ArrayList<Color>());
        politicCards.add(Color.BLACK);
        politicCards.add(Color.PURPLE);
    }
    public ArrayList<Color> getPoliticCards() {
        return politicCards;
    }
    public void setPoliticCards(ArrayList<Color> politicCards) {
        this.politicCards = politicCards;
    }



}
public class DrawPoliticCard {
    public DrawPoliticCard(Player player){
        PoliticCard politicCard = new PoliticCard();//I know that to 
//call a method from another class you should create an instance,
//but isn't *new PoliticCard();* creating a new arraylist in PoliticCard()?, 
//what i want is to create the arraylist only once (like it's written in the test below) 
//and then use the same updated arraylist each time i use this constructor
        player.getPoliticCards().add(politicCard.getPoliticCards().get(0));
        politicCard.getPoliticCards().remove(0);

    }

}
public class ModelPlayerTest {

    @Test
    public void testDrawCard() {
        Player player = new Player(1);
        new PoliticCard();
        new DrawPoliticCard(player);
        assertNotNull(player.getPoliticCards().get(0));
    }


}

3 个答案:

答案 0 :(得分:1)

从类中调用方法而不使用它的实例的唯一方法是调用静态方法。

答案 1 :(得分:1)

以下是如何实现这一目标:

// Fully construct the ArrayList here and it gets created just once per instance.
private ArrayList<Color> politicCards = new ArrayList<Color>()
public PoliticCard() {

    // Get rid of this call
    // setPoliticCards(new ArrayList<Color>());
    politicCards.add(Color.BLACK);
    politicCards.add(Color.PURPLE);
}

修改

在DrawPoliticCard类中创建一个PoliticCard实例,让每个玩家从同一个实例中绘制。

public class DrawPoliticCard
{
    final static PoliticCard politicCard = new PoliticCard();
    public static void drawCard(Player player)
    {
        player.getPoliticCards().add(politicCard.getPoliticCards().get(0));
        politicCard.getPoliticCards().remove(0);
    }
}

<强>用法

public class ModelPlayerTest {

    @Test
    public void testDrawCard() {
        Player player = new Player(1);
        Player player2 = new Player(2);

        // draw a card for player one
        DrawPoliticCard.drawCard(player);

        // draw a card for player two
        DrawPoliticCard.drawCard(player2);
    }
}

答案 2 :(得分:0)

您可以为PoliticCard

实施单例
private static PoliticCard instance = null;
public static PoliticCard getInstance()
{
    if (instance == null)
        instance = new PoliticCard();
    return instance;    
}

然后使用new PoliticCard();而不是调用PoliticCard.getInstance();来确保您只创建PoliticCard

的1个实例