java - How do I add an Object to a enum defined class? -
i trying follow code oracle docs show how use java enums. however, trying add "clown" in final protodeck
since each deck of 52 cards can contain additional "clown" or "joker" card. cannot add clown suit or rank because none of them.
i tried creating new enum
as
public enum clown {clown}
and tried put inside protodeck not work
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 { (suit suit : suit.values()) (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 } }
make card
interface.
you can link clown
in same group other cards, make behave appropriately in these situations too. you'd have like:
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. }
Comments
Post a Comment