enum public enum Week Sunday Moday Tuesday Wednesday
enum public enum Week {Sunday, Moday, Tuesday, Wednesday, Thursday, Friday, Saturday} Week. Sunday. ordinal() 0 Week. Sunday “Sunday”
enum 不可繼承 1. public enum Suites exten Enum{ 2. } 可實作 interface 不能放在方法中 所有成員為 static final 可以用==或equals來比較 Compiler會自動加入private final 於列值中
Enum An enum specifies a list of constant values assigned to a type. An enum is NOT a String or an int. An enum constant's type is the enum type. An enum can be declared outside or inside a class, but NOT in a method. You can NEVER invoke an enum constructor directly.
Enum The enum constructor is invoked automatically, with the arguments you define after the constant value. You can define more than one argument to the constructor, and you can overload the enum constructors An enum declared outside a class must NOT be marked static, final, abstract, protected, or private.
Enum enum constants can send arguments to the enum constructor, using the syntax BIG(8). Compiling an enum generates a. class file whose name is derived from the enum's name. The semicolon at the end of an enum declaration is optional. These are legal: enum Foo { ONE, TWO, THREE}; Foo. values() returns an array of My. Enum's values.
Declaring Enums enum Problem { BIG, HUGE, SMALL } // this cannot be private or protected class My. Class { Problem size; } public class Main. Class { public static void main(String[] args) { My. Class drink = new My. Class(); drink. size = Problem. BIG; } }
An example of declaring an enum inside a class My. Class 2 { enum Problem {BIG, HUGE, SMALL } Problem size; } public class Main. Class { public static void main(String[] args) { My. Class 2 drink = new My. Class 2(); drink. size = My. Class 2. Problem. BIG; // enclosing class name requir ed } }
You cannot declare enums in a method enum Problem {BIG, HUGE, SMALL } class My. Class { Problem size; } public class Main. Class { public static void main(String[] args) { enum Problem { BIG, HUGE, SMALL } // WRONG! My. Class drink = new My. Class(); drink. size = Problem. BIG; } } Exception in thread "main" java. lang. Error: Unresolved compilation problem: The member enum Problem cannot be local at Main. Class. main(Main. Class. java: 9)
enum Problem { // 8, 10 & 16 are passed to the constructor BIG(8), HUGE(10), SMALL(16); Problem(int ounces) { // constructor this. ounces = ounces; } private int ounces; public int get. Ounces() { return ounces; } } class My. Class { Problem size; public static void main(String[] args) { My. Class drink 1 = new My. Class(); drink 1. size = Problem. BIG; My. Class drink 2 = new My. Class(); drink 2. size = Problem. SMALL; System. out. println(drink 1. size. get. Ounces()); for(Problem cs: Problem. values()) System. out. println(cs + " " + cs. get. Ounces()); } 8 BIG 8 HUGE 10 SMALL 16
Enums may have main() methods and can be invoked as applications. enum Light. State { RED, YELLOW, GREEN; public static void main(String[] argv) { System. out. println(Light. State. RED. to. String()); } }
Enums have built-in name() and to. String() methods, both of which return the name of the current instance. public class Main. Class { public static void main(String[] argv) { System. out. println(Light. State. RED. name()); } } enum Light. State { RED, YELLOW, GREEN; }
You can add data, methods, and constructors to an enum Suit { DIAMOND(true), HEART(true), CLUB(false), SPADE(false); private boolean red; Suit(boolean b) { red = b; } public boolean is. Red() { return red; } public String to. String() { String s = name(); s += red ? ": red" : ": black"; return s; } }
- Slides: 12