Object-Oriented Programming - Lecture 3-bis: Enum types - Lê Hồng Phương

Enum types ● An enum type is a type whose fields consist of a fixed set of constants. – Compass directions – The days of the week – The suits of a card ● Because they are constants, the names of an enum type's fields are in uppercase letters.

pdf8 trang | Chia sẻ: candy98 | Lượt xem: 523 | Lượt tải: 0download
Bạn đang xem nội dung tài liệu Object-Oriented Programming - Lecture 3-bis: Enum types - Lê Hồng Phương, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Lecture 3-bis: Enum types Lê Hồng Phương phuonglh@gmail.com Department of Mathematics, Mechanics and Informatics, Vietnam National University, Hanoi 2012-2013 Object-Oriented Programming: Enum types 2 Enum types ● An enum type is a type whose fields consist of a fixed set of constants. – Compass directions – The days of the week – The suits of a card ● Because they are constants, the names of an enum type's fields are in uppercase letters. 2012-2013 Object-Oriented Programming: Enum types 3 Enum types public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } public enum CompassDirection { NORTH, SOUTH, EAST, WEST } public enum Suit { SPADE, CLUB, DIAMOND, HEART } 2012-2013 Object-Oriented Programming: Enum types 4 The Card class revisited public class Card { private int rank; private String suit; public Card(int rank, String suit) { this.rank = rank; this.suit = suit; } // ... } public class CardTester { public static void main(String[] args) { Card c1 = new Card(2, "Spade"); Card c2 = new Card(2, "Undefined"); } } We should preventuser from doing this. 2012-2013 Object-Oriented Programming: Enum types 5 The Card class revisited public class Card { private int rank; private Suit suit; public Card(int rank, Suit suit) { this.rank = rank; this.suit = suit; } // ... } public class CardTester { public static void main(String[] args) { Card c1 = new Card(1, Suit.SPADE); } } There are only 4 possible suits that a card can take. 2012-2013 Object-Oriented Programming: Enum types 6 Using an enum type public class DayTester { private Day day; public DayTester(Day day) { this.day = day; } public void tell() { switch (day) { case MONDAY: System.out.println("Mondays are bad."); break; case FRIDAY: System.out.println("Fridays are better."); break; case SATURDAY: case SUNDAY: System.out.println("Weekends are best."); break; default: System.out.println("Midweek days are so-so."); break; } } 2012-2013 Object-Oriented Programming: Enum types 7 Using an enum type public static void main(String[] args) { DayTester firstDay = new DayTester(Day.MONDAY); firstDay.tell(); DayTester thirdDay = new DayTester(Day.WEDNESDAY); thirdDay.tell(); DayTester fifthDay = new DayTester(Day.FRIDAY); fifthDay.tell(); DayTester sixthDay = new DayTester(Day.SATURDAY); sixthDay.tell(); DayTester seventhDay = new DayTester(Day.SUNDAY); seventhDay.tell(); } } // end of the class DayTester Mondays are bad. Midweek days are so-so. Fridays are better. Weekends are best. Weekends are best. 2012-2013 Object-Oriented Programming: Enum types 8 Using an enum type ● In Java, an enum type also defines a class. – The enum class body can include methods and other fields. ● The compiler automatically adds some special methods when it creates an enum. – The static values() method that returns an array containing all of the values of the enum in the order they are declared. Day[] days = Day.values(); for (Day d : days) { System.out.println(d); } SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY