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

Java在不创建实例的情况下调用方法,java,arraylist,instance,Java,Arraylist,Instance,请原谅,如果这个问题对于一个普通的java程序员来说很愚蠢,但是我被这个问题困住了。 我想从类DrawCard(Player)中的类PoliticCard调用方法getPoliticCards()。起初,我在politicalcard中使用了静态数组列表,因此我没有问题,但我不得不更改它,因为我应该能够同时运行游戏的多个会话 public enum Color { BLACK, PURPLE } public class Player { private int id;

请原谅,如果这个问题对于一个普通的java程序员来说很愚蠢,但是我被这个问题困住了。 我想从类
DrawCard(Player)
中的类
PoliticCard
调用方法
getPoliticCards()
。起初,我在
politicalcard
中使用了
静态数组列表
,因此我没有问题,但我不得不更改它,因为我应该能够同时运行游戏的多个会话

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


}
公共枚举颜色{
黑色,紫色
}
公开课选手{
私有int-id;
私人ArrayList政治卡;
公共播放器(int-id){
这个.setId(id);
ArrayList数组=新的ArrayList();
集合卡(阵列);
}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共阵列列表getPoliticalCards(){
归还信用卡;
}
公共无效政治卡(ArrayList政治卡){
this.politicCards=politicCards;
}
}
公共类政治卡{
私人ArrayList政治卡;
公共卡(){
setPoliticCards(新的ArrayList());
政治卡片。添加(颜色。黑色);
政治卡片。添加(颜色。紫色);
}
公共阵列列表getPoliticalCards(){
归还信用卡;
}
公共无效政治卡(ArrayList政治卡){
this.politicCards=politicCards;
}
}
公共类提款卡{
公共提款卡(玩家){
PoliticCard PoliticCard=newpoliticcard();//我知道
//从另一个类调用一个方法您应该创建一个实例,
//但是,*新政治卡();*在政治卡()中创建新的arraylist不是吗?,
//我想要的是只创建一次arraylist(就像下面的测试中编写的那样)
//然后每次使用这个构造函数时都使用相同的更新的arraylist
player.getPoliticCards().add(politicCard.getPoliticCards().get(0));
politicCard.getPoliticCards().remove(0);
}
}
公共类ModelPlayerTest{
@试验
公共提款卡(){
玩家=新玩家(1);
新卡片();
新抽卡(玩家);
assertNotNull(player.getPoliticCards().get(0));
}
}

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

以下是实现此目的的方法:

// 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);
}
用法

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

您可以为
politicalcard
实现单例:

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

然后,而不是调用
newcard()使用
PoliticCard.getInstance()
这将确保您只创建一个
PoliticCard

的实例,所以问题是?@PriyankaKotari我想在
PoliticCard PoliticCard=new PoliticCard()中替换
new PoliticCard()
DrawCard
中使用构造函数
new politicalcard()
时,不是额外添加了两张卡片吗?@mpz您希望politicalcard的所有实例共享ArrayList还是每个实例都有自己的?请澄清。我想我会用这个例子更好地解释:假设构造器
politicalcard
创建一个包含5张黑卡和5张紫色卡的arraylist,当我使用
DrawCard
时,第一个玩家的arraylist将添加一张黑卡,
politicalcard
的arraylist将包含4张黑卡,然后,当我对第二个玩家使用
DrawCard
时,他的arraylist将添加一张黑卡,
PoliticCard
的arraylist将添加三张黑卡,依此类推。它仍然使用静态属性
private static PoliticCard instance = null;
public static PoliticCard getInstance()
{
    if (instance == null)
        instance = new PoliticCard();
    return instance;    
}