声明arraylist对象(java)

时间:2016-02-20 20:56:37

标签: java object arraylist

基本上我正在制作二十一点游戏,所以我做的第一件事就是将对象设为“卡片”。

public class Card {

  int number;
  int suit;

  public static final String[] NUMBERS = {null, null, "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};
  public static final String[] SUITS = {"Diamonds", "Clubs", "Spades", "Hearts"};

  public Card(int number, int suit) {
    this.number = number;
    this.suit = suit;
  }

  @Override
  public String toString() {
    return NUMBERS[number] + " of " + SUITS[suit];
  }
}

接下来我做的是制作名为cardlist的卡片列表,然后是名为deck的卡片,这是一张带有标准卡片的卡片列表:

import java.util.ArrayList;

public class CardList {

  public String name;
  public ArrayList<Card> cards;

  public CardList(String name) {
    this.name = name;
    this.cards = new ArrayList<Card>();
  }

  public String getName() {
    return name;
  }

  public Card getCard(int i) {
    return cards.get(i);
  }

  public int howManyCards() {
    return cards.size();
  }

  public void addCard(Card card) {
    cards.add(card);
  }
}

然后是甲板:

public class Deck extends CardList {

  public Deck(String name) {
    super(name);

    for (int i = 0; i < 4; i++) {
      for (int j = 2; j < 15; j++) {
        addCard(new Card(j,i));
      }
    }
  }
}

之后我开始制作这样的游戏:

import java.util.Scanner;

public class BlackJack {

  public static String[] getNames(int players) {

    System.out.println("What are the names of the players?");
    String[] playerNames = new String[players];
    Scanner input = new Scanner(System.in);

    for (int i = 0; i < players; i++) {
      playerNames[i] = input.next();
    }
    return playerNames;
  }

  public static int peoplePlaying() {

    System.out.println("How many people are playing?");

    Scanner input = new Scanner(System.in);
    int players = input.nextInt();

    return players;
  }

  public static void main(String[] args) {

    int players = peoplePlaying();

    String[] playerNames = getNames(players);

    Deck deck = new Deck;

    System.out.println(deck);

  }
}

倒数第二行是我未能尝试声明一个套牌然后与程序的其余部分一起使用。由于deck是一个数组列表,我不知道是否正确声明它。当我创建对象Deck时,我做了它只需要一个字符串作为其名称,并且已经制作了52张卡但是我不确定它是如何工作的,你可以告诉。

非常感谢任何帮助

2 个答案:

答案 0 :(得分:1)

您已定义构造函数

public Deck(String name) 

所以要创建一个Deck,你需要给它一个像

这样的名字
Deck deck = new Deck(PlayerName[0]);

这是在IDE中使用自动完成的地方,将指导您完成此操作。

答案 1 :(得分:0)

Deck扩展CardList,CardList需要&#34; String name&#34;构造函数中的参数。这个结构显然是为了,玩家可以拥有cardList,deck是一个cardList等等......所以如果你想摆脱传递那个参数,你的代码应该是这样的,

public Deck() {
   this("default_deck_name");
   //this will call the current Class' constructor which has a String Parameter, --> Deck(String name) 
}

public Deck(String name) 
{
   super(name);
   //this will call the Parent Class(CardList)'s constructor which has a String parameter

   for (int i = 0; i < 4; i++) {
     for (int j = 2; j < 15; j++) {
       addCard(new Card(j,i));
     }
   }
} 

这样你就不需要在创建Deck时传递name参数

Deck defaultDeck = new Deck(); // this will create Deck instance with name default_deck_name

如果您想创建具有特定名称的Deck,您可以使用下面的代码

Deck defaultDeck = new Deck("specific_name");

这称为构造函数重载