- 在接口內(nèi)部,創(chuàng)建實(shí)現(xiàn)該接口的枚舉逼泣,這些枚舉都是該接口的子類趴泌,這些枚舉的實(shí)例都可以用該接口的變量接著,同時(shí)也是對(duì)實(shí)現(xiàn)同一接口的枚舉實(shí)例的分類拉庶;
package chapter19._7;
public interface Food {
enum Appetizer implements Food {
SALAD, SOUP, SPRING_ROLLS;
}
enum MainCourse implements Food {
LASAGNE, BURRITO, PAD_THAI, LENTILS, HUMMOUS, VINDALOO;
}
enum Dessert implements Food {
TIRAMISSU, GELTO, BLACK_FOREST_CAKE, FRUIT, CREAM_CARAMEL;
}
enum Coffee implements Food {
BLACK_COFFEE, DECAF_COFFEE, ESPRESSO, LATTE, CAPPUCCINO, TEA, HERB_TEA;
}
}
- 因?yàn)檫@些枚舉(Appetizer嗜憔,MainCourse,Dessert氏仗,Coffee)都實(shí)現(xiàn)了Food接口吉捶, 所以這些枚舉的實(shí)例都能用Food的變量接著;
package chapter19._7;
import static chapter19._7.Food.*;
public class TypeOfFood {
public static void main(String[] args) {
Food food = Appetizer.SALAD;
food = MainCourse.LASAGNE;
food = Dessert.GELTO;
food = Coffee.CAPPUCCINO;
}
}
- 枚舉實(shí)例APPETIZER等需要一個(gè)參數(shù)為Class<Appetizer>類型的構(gòu)造方法皆尔,由該構(gòu)造方法構(gòu)造完成后呐舔,APPETIZER將擁有一個(gè)數(shù)組,一個(gè)全是Appetizer實(shí)例的數(shù)組慷蠕;
- 構(gòu)造函數(shù)和Course的enum實(shí)例(APPETIZER珊拼,MAINCOURSE,DESSERT流炕,COFFEE)是只能對(duì)應(yīng)其中一個(gè)的關(guān)系澎现;
package chapter19._7;
import chapter19._6.Enums;
public enum Course {
APPETIZER(Food.Appetizer.class),
MAINCOURSE(Food.MainCourse.class),
DESSERT(Food.Dessert.class),
COFFEE(Food.Coffee.class);
private Food[] values;
private Course(Class<? extends Food> kind) {
this.values = kind.getEnumConstants();
}
public Food randomSelection() {
return Enums.random(values);
}
}
- 每個(gè)Course實(shí)例都有一個(gè)數(shù)組Food[]仅胞,因?yàn)锳ppetizer,MainCourse剑辫,Dessert干旧,Coffee都實(shí)現(xiàn)了Food接口,他們的實(shí)例都可以用Food的變量接著妹蔽;
package chapter19._7;
public class Meal {
public static void main(String[] args) {
for(int i = 0; i < 5; i++) {
for (Course course : Course.values()) {
Food food = course.randomSelection();
System.out.println(food);
}
System.out.println("----------");
}
}
}
輸出:
SPRING_ROLLS
VINDALOO
FRUIT
DECAF_COFFEE
++++++++++++++++++++
SOUP
VINDALOO
FRUIT
TEA
++++++++++++++++++++
SALAD
BURRITO
FRUIT
TEA
++++++++++++++++++++
SALAD
BURRITO
CREAM_CARAMEL
LATTE
++++++++++++++++++++
SOUP
BURRITO
TIRAMISSU
ESPRESSO
++++++++++++++++++++
- Stock和Bond實(shí)現(xiàn)了Security椎眯,則Stock和Bond的每個(gè)實(shí)例都可以用Security的變量接著;
- enum實(shí)例可以用enum類的變量接著:SecurityCategory category = Enums.random(SecurityCategory.class);
package chapter19._7;
import chapter19._6.Enums;
public enum SecurityCategory {
STOCK(Security.Stock.class), Bond(Security.Bond.class);
Security[] values;
SecurityCategory(Class<? extends Security> kind) {
this.values = kind.getEnumConstants();
}
interface Security {
enum Stock implements Security { SHORT, LONG, MARGIN }
enum Bond implements Security { MUNICIPAL, JUNK }
}
public Security randomSelection() {
return Enums.random(values);
}
public static void main(String[] args) {
for(int i = 0; i < 10; i++) {
SecurityCategory category = Enums.random(SecurityCategory.class);
System.out.println(category + ": " + category.randomSelection());
}
}
}
輸出:
Bond: MUNICIPAL
Bond: MUNICIPAL
STOCK: MARGIN
STOCK: MARGIN
Bond: JUNK
STOCK: SHORT
STOCK: LONG
STOCK: LONG
Bond: MUNICIPAL
Bond: JUNK