public enum Color { RED, BLUE, YELLOW, PURPLE }
注意 :RED,BLUE 这些是由 enum Color类调用默认private构造器创建的实例对象,和普通class相比只不过enum的实例对象只能在内部创建。时刻记着他们是一个实例对象。
Color red = Color.RED;
Color blue = Color.BLUE;
public enum Color { RED("这是红色"), BLUE("这是蓝色"), YELLOW("这是黄色"), PURPLE("这是紫色"); String des; Color( String s) { this.des = s; } public String getDes(){ return des; } }
调用 getDes( )方法就可以的到当前对象的描述信息。
重写toString( )方法来为enum实例添加描述信息。
public enum Color { RED, BLUE, YELLOW, PURPLE; @Override public String toString() { String id = name(); return id + " is " + id.toLowerCase(); } }
通过name() 拿到当前对象名字。
public interface Food { enum Fruit implements Food{ APPLE, BANANA, ORANGE; } enum Vegetables implements Food{ TOMATO, POTATO, BEANS; } enum Drink implements Food{ COFFEE, COCA, REDBULL; } } public static void main(String[] args) { Food food = Food.Fruit.APPLE; food = Food.Drink.REDBULL; food = Food.Vegetables.BEANS; }
public enum Course { FRUIT(Food.Fruit.class), DRINK(Food.Drink.class), VEGETABLES(Food.Vegetables.class); private Food[] values; Course(Class<? extends Food> kind) { this.values = kind.getEnumConstants(); } public Food[] getValues() { return values; } }
通过实例调用getValues方法就可以的到该实例的所有元素。
public enum Course { FRUIT(Food.Fruit.class), DRINK(Food.Drink.class), VEGETABLES(Food.Vegetables.class); private Food[] values; Course(Class<? extends Food > kind) { this.values = kind.getEnumConstants(); } interface Food { enum Fruit implements Food { APPLE, BANANA, ORANGE; } enum Vegetables implements Food { TOMATO, POTATO, BEANS; } enum Drink implements Food { COFFEE, COCA, REDBULL; } } public Food[] getValues() { return values; } }
EnumSet<Color> enumSet = EnumSet.noneOf(Color.class); //创建一个空集 EnumSet<Color> enumSet2 = EnumSet.allOf(Color.class); //把集合中所有元素添加进去 EnumSet<Color> enumSet3 = EnumSet.of(RED);//添加一个元素
EnumSet不止这几个方法,对于of() 方法重载了6次,当传入2-5个参数调用相应方法,传入1个或者5个以上调用可变参数。
RED{ @Override String getInfo() { return null; } @Override String getTime() { return null; } }; abstract String getInfo(); abstract String getTime();
import java.util.EnumSet; import java.util.Random; public enum COR { SOLUTION_ONE{ @Override boolean Solve(int i) { if (i == 1){ System.out.println(name()+" 解决问题 " +i); return true; } return false; } }, SOLUTION_TWO{ @Override boolean Solve(int i) { if (i == 2){ System.out.println(name()+" 解决问题 " +i); return true; }return false; } }, SOLUTION_THREE{ @Override boolean Solve(int i) { if (i == 3){ System.out.println(name()+" 解决问题 " +i); return true; }return false; } }, SOLUTION_FOUR{ @Override boolean Solve(int i) { if (i == 4){ System.out.println(name()+" 可以解决问题 " +i); return true; }return false; } }; abstract boolean Solve(int i); public static void main(String[] args) { Random random = new Random(); EnumSet<COR> cors = EnumSet.allOf(COR.class); for (int i = 0; i < 6; i++) { int id = random.nextInt(4)+1; for (COR cor :cors) { if (cor.Solve(id)){ System.out.println(" 解决问题 " +id); break; } } } } }
public enum Outcome { WIN, LOSE, DRAW } ///:~ interface Item { Outcome compete(Item it); Outcome eval(Paper p); Outcome eval(Scissors s); Outcome eval(Rock r); } class Paper implements Item { public Outcome compete(Item it) { return it.eval(this); } public Outcome eval(Paper p) { return DRAW; } public Outcome eval(Scissors s) { return WIN; } public Outcome eval(Rock r) { return LOSE; } public String toString() { return "Paper"; } } class Scissors implements Item { public Outcome compete(Item it) { return it.eval(this); } public Outcome eval(Paper p) { return LOSE; } public Outcome eval(Scissors s) { return DRAW; } public Outcome eval(Rock r) { return WIN; } public String toString() { return "Scissors"; } } class Rock implements Item { public Outcome compete(Item it) { return it.eval(this); } public Outcome eval(Paper p) { return WIN; } public Outcome eval(Scissors s) { return LOSE; } public Outcome eval(Rock r) { return DRAW; } public String toString() { return "Rock"; } } public class RoShamBo1 { static final int SIZE = 20; private static Random rand = new Random(47); public static Item newItem() { switch (rand.nextInt(3)) { default: case 0: return new Scissors(); case 1: return new Paper(); case 2: return new Rock(); } } public static void match(Item a, Item b) { System.out.println(a + " vs. " + b + ": " + a.compete(b)); } public static void main(String[] args) { for (int i = 0; i < SIZE; i++) match(newItem(), newItem()); } }
package enums; import static enums.OutCome.*; public enum RoSham { PAPER(DRAW, LOSE, WIN), SCISSORS(WIN, DRAW, LOSE), ROCK(LOSE, WIN, DRAW); private OutCome vPAPER, vSCISSORS, vROCK; RoSham(OutCome paper, OutCome scissors, OutCome rock) { this.vPAPER = paper; this.vSCISSORS = scissors; this.vROCK = rock; } public OutCome complete(RoSham it) { switch (it) { default: case PAPER: return vPAPER; case SCISSORS: return vSCISSORS; case ROCK: return vROCK; } } public static void main(String[] args) { System.out.println(PAPER.complete(ROCK)); } }
PAPER.complete()时把PAPER构造器中的结果与 OutCome 变量绑定,根据对比的参数返回对比结果,因此实例构造器中的参数位置非常重要、
package enums; import java.util.EnumMap; import static enums.OutCome.*; public enum RoShamBo { PAPER, SCISSORS, ROCK; static EnumMap<RoShamBo, EnumMap<RoShamBo, OutCome>> table = new EnumMap<RoShamBo, EnumMap<RoShamBo, OutCome>>(RoShamBo.class); static { for (RoShamBo it : RoShamBo.values()) { table.put(it, new EnumMap<RoShamBo, OutCome>(RoShamBo.class)); } initRow(PAPER, DRAW, LOSE, WIN); initRow(SCISSORS, WIN, DRAW, LOSE); initRow(ROCK, LOSE, WIN, DRAW); } static void initRow(RoShamBo it, OutCome vPAPER, OutCome vSCISSORS, OutCome vROCK) { EnumMap<RoShamBo, OutCome> row = RoShamBo.table.get(it); row.put(RoShamBo.PAPER, vPAPER); row.put(RoShamBo.SCISSORS, vSCISSORS); row.put(RoShamBo.ROCK, vROCK); } public OutCome complete(RoShamBo it) { return table.get(this).get(it); } public static void main(String[] args) { System.out.println(ROCK.complete(SCISSORS)); } }
complete方法实现了2次分发
private static OutCome[][] tables = { {DRAW, LOSE, WIN}, {WIN, DRAW, LOSE}, {LOSE, WIN, DRAW}, }; public OutCome completes (RoShamBo other) { return tables[this.ordinal()][other.ordinal()]; }
原文:https://www.cnblogs.com/mibloom/p/9508077.html