Java Tutorial/Data Type/enum methods
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
- 1 Adding Members to an Enumeration Class
- 2 compareTo() and equals() for enum data type
- 3 Demonstrate ordinal(), compareTo(), and equals().
- 4 Obtaining all ordinal values using ordinal()
- 5 Switch statement with enum
- 6 Use an enum constructor, instance variable, and method.
- 7 Use the built-in enumeration methods.
- 8 Using the built-in enumeration methods: values( )
- 9 Using valueOf()
Adding Members to an Enumeration Class
enum JacketSize {
small(36), medium(40), large(42), extra_large(46), extra_extra_large(48);
JacketSize(int chestSize) {
this.chestSize = chestSize;
}
public int chestSize() {
return chestSize;
}
private int chestSize;
}
class Jacket {
public Jacket(JacketSize size) {
this.size = size;
}
public String toString() {
switch (this.size) {
case small:
return "S";
case medium:
return "M";
case large:
return "L";
case extra_large:
return "XL";
case extra_extra_large:
return "XXL";
default:
return "";
}
}
private JacketSize size;
}
public class MainClass {
public static void main(String[] args) {
Jacket[] jackets = { new Jacket(JacketSize.medium),
new Jacket(JacketSize.extra_large),
new Jacket(JacketSize.small),
new Jacket(JacketSize.extra_extra_large) };
System.out.println("\n\nJackets sizes available are:\n");
for (JacketSize size : JacketSize.values()) {
System.out.print(" " + size);
}
System.out.println("\n\nJackets in stock are:");
for (Jacket jacket : jackets) {
System.out.println(jacket);
}
}
}
Jackets sizes available are: small medium large extra_large extra_extra_large Jackets in stock are: M XL S XXL
compareTo() and equals() for enum data type
enum Week {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturaday, Sunday
}
public class MainClass {
public static void main(String args[]) {
Week day1, day2, day3;
day1 = Week.Monday;
day2 = Week.Tuesday;
day3 = Week.Friday;
//
if (day1.rupareTo(day2) < 0)
System.out.println(day1 + " comes before " + day2);
if (day2.rupareTo(day3) > 0)
System.out.println(day2 + " comes before " + day3);
if (day1.rupareTo(day3) == 0)
System.out.println(day1 + " equals " + day3);
}
}
Monday comes before Tuesday
Demonstrate ordinal(), compareTo(), and equals().
enum Apple {
A, B, C, D, E
}
class EnumDemo4 {
public static void main(String args[]) {
Apple ap, ap2, ap3;
for (Apple a : Apple.values())
System.out.println(a + " " + a.ordinal());
ap = Apple.C;
ap2 = Apple.B;
ap3 = Apple.C;
System.out.println();
if (ap.rupareTo(ap2) < 0)
System.out.println(ap + " comes before " + ap2);
if (ap.rupareTo(ap2) > 0)
System.out.println(ap2 + " comes before " + ap);
if (ap.rupareTo(ap3) == 0)
System.out.println(ap + " equals " + ap3);
System.out.println();
if (ap.equals(ap2))
System.out.println("Error!");
if (ap.equals(ap3))
System.out.println(ap + " equals " + ap3);
if (ap == ap3)
System.out.println(ap + " == " + ap3);
}
}
Obtaining all ordinal values using ordinal()
enum Week {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturaday, Sunday
}
public class MainClass {
public static void main(String args[]) {
// Obtain all ordinal values using ordinal().
System.out.println("Here are all week constants" + " and their ordinal values: ");
for (Week day : Week.values())
System.out.println(day + " " + day.ordinal());
}
}
Here are all week constants and their ordinal values: Monday 0 Tuesday 1 Wednesday 2 Thursday 3 Friday 4 Saturaday 5 Sunday 6
Switch statement with enum
public class MainClass {
enum Choice { Choice1, Choice2, Choice3 }
public static void main(String[] args) {
Choice ch = Choice.Choice1;
switch(ch) {
case Choice1:
System.out.println("Choice1 selected");
break;
case Choice2:
System.out.println("Choice2 selected");
break;
case Choice3:
System.out.println("Choice3 selected");
break;
}
}
}
Choice1 selected
Use an enum constructor, instance variable, and method.
enum Apple {
A(10), B(9), C(12), D(15), E(8);
private int price; // price of each apple
Apple(int p) {
price = p;
}
int getPrice() {
return price;
}
}
class EnumDemo3 {
public static void main(String args[]) {
Apple ap;
System.out.println("D costs " + Apple.D.getPrice() + " cents.\n");
System.out.println("All apple prices:");
for (Apple a : Apple.values())
System.out.println(a + " costs " + a.getPrice() + " cents.");
}
}
Use the built-in enumeration methods.
enum Apple {
A, B, C, D, E
}
class EnumDemo2 {
public static void main(String args[]) {
Apple ap;
System.out.println("Here are all Apple constants:");
// use values()
Apple allapples[] = Apple.values();
for (Apple a : allapples)
System.out.println(a);
System.out.println();
// use valueOf()
ap = Apple.valueOf("D");
System.out.println("ap contains " + ap);
}
}
Using the built-in enumeration methods: values( )
All enumerations automatically contain two predefined methods: values( ) and valueOf( ).
- public static enum-type[ ] values( )
- public static enum-type valueOf(String str)
The values( ) method returns an array that contains a list of the enumeration constants.
The valueOf( ) method returns the enumeration constant whose value corresponds to the string passed in str.
enum Week {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturaday, Sunday
}
public class MainClass {
public static void main(String args[]) {
System.out.println("Here are all Week constants");
// use values()
Week allWeek[] = Week.values();
for (Week aday : allWeek) {
System.out.println(aday);
}
}
}
Here are all Week constants Monday Tuesday Wednesday Thursday Friday Saturaday Sunday
Using valueOf()
enum Week {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturaday, Sunday
}
public class MainClass {
public static void main(String args[]) {
Week day;
//
day = Week.valueOf("Monday");
System.out.println("day contains " + day);
}
}
day contains Monday