我正在尝试从Oracle文档中学习这些代码,向您展示如何使用Java ENUMS。但是,我正在尝试添加一个" Clown"在最后protoDeck
,因为每张牌的52张牌都可以包含额外的"小丑"或者"小丑"卡。我无法将CLOWN添加到SUIT或RANK中,因为它们都没有。
我尝试创建一个新的enum
作为
public enum Clown {Clown}
并尝试将其置于protoDeck中,但它不起作用
public class Card {
public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX,
SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE }
public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }
private final Rank rank;
private final Suit suit;
private Card(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
public Rank rank() { return rank; }
public Suit suit() { return suit; }
public String toString() { return rank + " of " + suit; }
private static final List<Card> protoDeck = new ArrayList<Card>();
// Initialize prototype deck
static {
for (Suit suit : Suit.values())
for (Rank rank : Rank.values())
protoDeck.add(new Card(rank, suit));
}
public static ArrayList<Card> newDeck() {
return new ArrayList<Card>(protoDeck); // Return copy of prototype deck
}
}
答案 0 :(得分:2)
Card
放入界面。您可以将Clown
与其他卡片放在同一组中,只是让它在这些情况下也能正常运行。所以你有类似的东西:
public interface Card {
Rank getRank();
Suit getSuit();
}
public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX,
SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE, OTHER }
public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES, NONE }
public class RegularCard implements Card {
private RegularCard(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
public Rank getRank() { return rank; }
public Suit getSuit() { return suit; }
}
public enum MiscellaneousCards implements Card {
CLOWN, JOKER;
// etc.
}
答案 1 :(得分:0)
感谢@KDM和@TAsk
我最终让它像这样工作:
public class CardSet {
// Create ENUM for both Suites and Ranks
public enum Ranks {TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE, CLOWN}
public enum Suits {SPADES, HEARTS, CLUBS, DIAMONDS, CLOWN}
private final Ranks rank;
private final Suits suit;
private int numOfDecks;
public CardSet(Suits suit, Ranks rank) {
this.rank = rank;
this.suit = suit;
}
private static final List<CardSet> deck = new ArrayList<CardSet>();
static {
for (Suits suit : Suits.values()) {
for (Ranks rank : Ranks.values()) {
if ((suit != Suits.CLOWN && rank != Ranks.CLOWN) || (suit == Suits.CLOWN && rank == Ranks.CLOWN)) {
deck.add(new CardSet(suit, rank));
}
}
}
}
public int getNumOfDecks(){
return this.numOfDecks;
}
public List<CardSet> getNewDeck(){
return new ArrayList<>(deck);
}
输出就是这样的,最后是CLOWN-CLOWN:
SPADES-TWO
SPADES-THREE
SPADES-FOUR
SPADES-FIVE
SPADES-SIX
SPADES-SEVEN
SPADES-EIGHT
SPADES-NINE
SPADES-TEN
SPADES-JACK
SPADES-QUEEN
SPADES-KING
SPADES-ACE
HEARTS-TWO
HEARTS-THREE
HEARTS-FOUR
HEARTS-FIVE
HEARTS-SIX
HEARTS-SEVEN
HEARTS-EIGHT
HEARTS-NINE
HEARTS-TEN
HEARTS-JACK
HEARTS-QUEEN
HEARTS-KING
HEARTS-ACE
CLUBS-TWO
CLUBS-THREE
CLUBS-FOUR
CLUBS-FIVE
CLUBS-SIX
CLUBS-SEVEN
CLUBS-EIGHT
CLUBS-NINE
CLUBS-TEN
CLUBS-JACK
CLUBS-QUEEN
CLUBS-KING
CLUBS-ACE
DIAMONDS-TWO
DIAMONDS-THREE
DIAMONDS-FOUR
DIAMONDS-FIVE
DIAMONDS-SIX
DIAMONDS-SEVEN
DIAMONDS-EIGHT
DIAMONDS-NINE
DIAMONDS-TEN
DIAMONDS-JACK
DIAMONDS-QUEEN
DIAMONDS-KING
DIAMONDS-ACE
CLOWN-CLOWN