|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectenum.Suit
Simple class demonstrating how to create an ordinal based typesafe enum based
around the suits in a deck of cards. There are four possible values in the enum's range:
HEARTS
CLUBS
SPADES
DIAMONDS
The big idea here is that you use this algorithm in place of using constant declarations.
For examples use this:
Suit mySuit = getSuit();
if(mySuit == Suit.DIAMONDS) {
// Do Something
}
and DON'T use
// Some Where in your code...
private static final String DIAMONDS = "diamonds";
private static final String HEARTS = "hearts";
private static final String CLUBS = "clubs";
private static final String SPADES = "spades";
:
:
String mySuit = getSuit();
if(mySuit.equals(DIAMONDS)) {
// Do something...
}
Note the use of the private constructor limiting you to the four suit types. you can't do:
Suit mySuit = new Suit("Wobbles");
... and the use of the final keyword so it's no to:
public class mySuit extends Suit {
| Field Summary | |
static Suit |
CLUBS
Four values for Suit are created at construction time and allocated to public member vars. |
static Suit |
DIAMONDS
Four values for Suit are created at construction time and allocated to public member vars. |
static Suit |
HEARTS
Four values for Suit are created at construction time and allocated to public member vars. |
private java.lang.String |
name
Somewhere to hold the name of the suit - "diamonds", "clubs" etc. |
private static int |
nextOrdinal
Ordinal of the next suit to be created |
private int |
ordinal
Assign the next ordinal to this suit |
static Suit |
SPADES
Four values for Suit are created at construction time and allocated to public member vars. |
| Constructor Summary | |
private |
Suit(java.lang.String name)
Constructor - Pass in the name of the Suit. |
| Method Summary | |
int |
compareTo(java.lang.Object o)
Compare the input parameter to this Suit class. |
static void |
main(java.lang.String[] args)
main Holds sample code that could be used in any client of an enum class The whole point of this is that you can used a == symbol to compare objects. |
java.lang.String |
toString()
Override toString() returning the Suit's name |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
private final java.lang.String name
private static int nextOrdinal
private final int ordinal
public static final Suit CLUBS
public static final Suit HEARTS
public static final Suit SPADES
public static final Suit DIAMONDS
| Constructor Detail |
private Suit(java.lang.String name)
| Method Detail |
public java.lang.String toString()
public int compareTo(java.lang.Object o)
compareTo in interface java.lang.Comparablepublic static void main(java.lang.String[] args)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||