OOP_L9.ppt
- Количество слайдов: 21
Перечислення в мові Java Перечислення – це список поіменованих констант В Java перечислення визначають тип класу. Перечислення можуть мати: q Конструктори q Методи q Змінні-екземпляри
Оголошення перечислень enum Apple { Jonathan, Golden. Del, Red. Del, Winesap, Cortland } Apple ap;
Використання перечислень Операція присвоєння Ap = Apple. Read. Del; Умовні вирази If (ap == Apple. Read. Del) // … Оператор switch(ap) { case Jonathan: // … Case Winesap: // … } Виведення System. out. println(Apple. Read. Del);
Використання перечислень enum Apple { Jonathan, Golden. Del, Red. Del, Winesap, Cortland } class Enum. Demo { public static void main(String args[]) { Apple ap; ap = Apple. Red. Del; // Output an enum value. System. out. println("Value of ap: " + ap); System. out. println(); ap = Apple. Golden. Del; // Compare two enum values. if(ap == Apple. Golden. Del) System. out. println("ap contains Golden. Del. n"); // Use an enum to control a switch statement. switch(ap) { case Jonathan: System. out. println("Jonathan is red. "); break; case Golden. Del: System. out. println("Golden Delicious is yellow. "); break; case Red. Del: System. out. println("Red Delicious is red. "); break; case Winesap: System. out. println("Winesap is red. "); break; case Cortland: System. out. println("Cortland is red. "); break; } } } Value of ap: Red. Del ap contains Golden. Del. Golden Delicious is yellow.
Методи values() та value. Of() (1) public static тип_enum[] values() public static тип_enum value. Of(String стрічка)
Методи values() та value. Of() (2) enum Apple { Jonathan, Golden. Del, Red. Del, Winesap, Cortland } class Enum. Demo 2 { public static void main(String args[]) { Apple ap; System. out. println("Here all Apple constants"); // use values() Apple allapples[] = Apple. values(); for(Apple a : allapples) System. out. println(a); System. out. println(); // use value. Of() ap = Apple. value. Of("Winesap"); System. out. println("ap contains " + ap); } } Here all Apple constants Jonathan Golden. Del Red. Del Winesap Cortland ap contains Winesap
Перечислення як тип класу (1) enum Apple { Jonathan(10), Golden. Del(9), Red. Del(12), Winesap(15), Cortland(8); private int price; // price of each apple // Constructor Apple(int p) { price = p; } int get. Price() { return price; } } class Enum. Demo 3 { public static void main(String args[]) { Apple ap; // Display price of Winesap. System. out. println("Winesap costs " + Apple. Winesap. get. Price() + " cents. n"); // Display all apples and prices. System. out. println("All apple prices: "); for(Apple a : Apple. values()) System. out. println(a + " costs " + a. get. Price() + " cents. "); } } Winesap costs 15 cents. All apple prices: Jonathan costs 10 cents. Golden. Del costs 9 cents. Red. Del costs 12 cents. Winesap costs 15 cents. Cortland costs 8 cents.
Перечислення як тип класу (2) enum Apple { Jonathan(10), Golden. Del(9), Red. Del, Winesap(15), Cortland(8); private int price; // price of each apple // Constructor Apple(int p) { price = p; } // Overloaded constructor Apple() { price = -1; } int get. Price() { return price; } }
Успадкування від Enum (1) final int ordinal() final int compare. To(тип_enum e) equals()
Успадкування від Enum (2) enum Apple { Jonathan, Golden. Del, Red. Del, Winesap, Cortland } class Enum. Demo 4 { public static void main(String args[]) { Apple ap, ap 2, ap 3; // Obtain all ordinal values using ordinal(). System. out. println("Here all apple constants" + " and their ordinal values: "); for(Apple a : Apple. values()) System. out. println(a + " " + a. ordinal()); ap = Apple. Red. Del; ap 2 = Apple. Golden. Del; ap 3 = Apple. Red. Del; System. out. println(); // Demonstrate compare. To() and equals() if(ap. compare. To(ap 2) < 0) System. out. println(ap + " comes before " + ap 2); if(ap. compare. To(ap 2) > 0) System. out. println(ap 2 + " comes before " + ap); if(ap. compare. To(ap 3) == 0) System. out. println(ap + " equals " + ap 3); System. out. println(); if(ap. equals(ap 2)) System. out. println("Error!"); if(ap. equals(ap 3)) System. out. println(ap + " equals " + ap 3); if(ap == ap 3) System. out. println(ap + " == " + ap 3); } } Here all apple and their ordinal values: Jonathan 0 Golden. Del 1. Red. Del costs 2 Winesap costs 3 Cortland 4 Golden. Del comes before Red. Del equals Red. Del == Red. Del
Оболонки типів 1. 2. 3. 4. 5. 6. 7. 8. Double Float Long Integer Short Byte Character Boolean
Оболонка типу сhar Конструктор Character (char ch) Метод сhar char. Value()
Оболонка типу boolean Конструктори Bolean(bolean bool. Value) Bolean(String bool. String) Метод boolean Value()
Оболонки числових типів Абстрактний тип Number Методи § byte. Value() § double. Value() § float. Value() § int. Value() § long. Value() § short. Value() Конструктори Integer(int num) Integer(String str)
Оболонки числових типів class Wrap { public static void main(String args[]) { Integer i. Ob = new Integer(100); int i = i. Ob. int. Value(); System. out. println(i + " " + i. Ob); // displays 100 } } 100
Автопакування та авторазпакування (1) Integer i. Ob = 100 int i = i. Ob
Автопакування та авторазпакування (2) class Auto. Box { public static void main(String args[]) { Integer i. Ob = 100; // autobox an int i = i. Ob; // auto-unbox System. out. println(i + " " + i. Ob); // displays 100 } } 100
Автопакування та авторазпакування (3) class Auto. Box 2 { // Take an Integer parameter and return // an int value; static int m(Integer v) { return v ; // auto-unbox to int } public static void main(String args[]) { // Pass an int to m() and assign the return value // to an Integer. Here, the argument 100 is autoboxed // into an Integer. The return value is also autoboxed // into an Integer i. Ob = m(100); System. out. println(i. Ob); } } 100
Автопакування та авторазпакування (4) class Auto. Box 3 { public static void main(String args[]) { Integer i. Ob, i. Ob 2; int i; i. Ob = 100; System. out. println("Original value of i. Ob: " + i. Ob); // The following automatically unboxes i. Ob, // performs the increment, and then reboxes // the result back into i. Ob. ++i. Ob; System. out. println("After ++i. Ob: " + i. Ob); // Here, i. Ob is unboxed, the expression is // evaluated, and the result is reboxed and // assigned to i. Ob 2 = i. Ob + (i. Ob / 3); System. out. println("i. Ob 2 after expression: " + i. Ob 2); // The same expression is evaluated, but the // result is not reboxed. i = i. Ob + (i. Ob / 3); System. out. println("i after expression: " + i); } }
Автопакування та авторазпакування (5) class Auto. Box 4 { public static void main(String args[]) { class Auto. Box 5 { public static void main(String args[]) { // Autobox/unbox a boolean. Boolean b = true; Integer i. Ob = 100; ; Double d. Ob = 98. 6; in d. Ob = d. Ob + i. Ob; System. out. println("d. Ob after expression: " + d. Ob); // Below, b is auto-unboxed when used // a conditional expression, such as an if. if(b) System. out. println("b is true"); // Autobox/unbox a char. Character ch = 'x'; // box a char ch 2 = ch; // unbox a char } } System. out. println("ch 2 is " + ch 2);
Автопакування та авторазпакування (6) class Unboxing. Error { public static void main(String args[]) { Integer i. Ob = 1000; // autobox the value 1000 int i = i. Ob. byte. Value(); // manually unbox as byte !!! System. out. println(i); // does not display 1000 ! } }


