问题
Please excuse me if this question looks dumb for a regular java programmer but i'm stuck with this problem.
I want to call the method getPoliticCards()
from the class PoliticCard
in the class DrawCard(Player player)
. At first i used a static arraylist
in PoliticCard
so i had no problems, but i had to change it because i'm supposed to be able to run several sessions of the game at the same time.
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));
}
}
回答1:
Here's how you can accomplish this:
// 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);
}
EDIT:
Create one PoliticCard instance in the DrawPoliticCard class and have each player draw from the same instance.
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);
}
}
Usage:
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:
The only way to invoke a method from a class without using a real instance of it is by invoking a static method.
回答3:
you can implement singleton for PoliticCard
:
private static PoliticCard instance = null;
public static PoliticCard getInstance()
{
if (instance == null)
instance = new PoliticCard();
return instance;
}
then instead of calling new PoliticCard();
use PoliticCard.getInstance();
that will make sure you only create 1 instance of PoliticCard
来源:https://stackoverflow.com/questions/37597778/java-calling-a-method-without-creating-an-instance