I understand the enumerations code as taught for java 1.5.0. However I don't understand exactly where we ought to put them, I mean:confused:: are they like classes? public enum Whatever{}? but do we need to write them INSIDE other classes? Or can we just aggregate them or something?
enumerations
Started by webdreamer, Nov 17 2007 04:12 AM
1 reply to this topic
#1
Posted 17 November 2007 - 04:12 AM
|
|
|
#2
Posted 19 November 2007 - 09:51 AM
An example of using enum:
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
}
}


Sign In
Create Account

Back to top









