枚举策略模式:
package com.price.effective.enum1; public enum Pay { MONDAY(PayType.WEEKDAY), SUNDAY(PayType.WEEKEND); private PayType payType; Pay(PayType payType){ this.payType = payType; } double pay(double hoursworked, double payRate){ return payType.overPay(hoursworked - 8, payRate) + payRate*8; } private enum PayType{ WEEKEND{ @Override double overPay(double hours, double payRate) { return hours * payRate * 3; } }, WEEKDAY{ @Override double overPay(double hours, double payRate) { return hours * payRate; } }; abstract double overPay(double hours, double payRate); } }
枚举值域
package com.price.effective.enum1; import java.util.EnumSet; import java.util.Set; public class TestEnumSet { // 一般的结合是List或者Set, 还有种跟好的办法如下: public static final int STYLE_BOLD = 1 << 0; public static final int STYLE_ITALIC = 1 << 1; // 这样就能够使用位运算来得到一个int型表示的集合,效率较高 public void applyStyles(int styles){ if((styles & STYLE_BOLD) != 0){ System.out.println("BOLD"); } if((styles & STYLE_ITALIC) != 0){ System.out.println("ITALIC"); } } // 下面用EnumSet来实现以下 public enum Style{BOLD, ITALIC}; public void applyStyles(Set<Style> styles){ if(styles.contains(Style.BOLD)){ System.out.println("BOLD"); } if(styles.contains(Style.ITALIC)){ System.out.println("ITALIC"); } } public static void main(String[] args) { TestEnumSet tes = new TestEnumSet(); tes.applyStyles(TestEnumSet.STYLE_BOLD | TestEnumSet.STYLE_ITALIC); tes.applyStyles(EnumSet.of(Style.BOLD, Style.ITALIC)); } }
枚举作为Map的key:
package com.price.effective.enum1; import java.util.ArrayList; import java.util.Collection; import java.util.EnumMap; import java.util.List; import java.util.Map; /** * 给动物分类 * * @author cdwangzijian * */ public class TestEnumMap { public static void main(String[] args) { // 一种方式是便利所有的动物,根据类型加入到对应的集合中 // 稍微好一点的可能可以利用类型名称数组来分类,如下: List<Animal>[] animalByType = new List[Animal.Type.values().length]; for (int i = 0 ; i < animalByType.length; i++) { animalByType[i] = new ArrayList<Animal>(); } for (Animal a : Animal.getAnimals()) { animalByType[a.getType().ordinal()].add(a); } printResult(animalByType); Map<Animal.Type, Animal> animalTypeMap = new EnumMap<Animal.Type, Animal>(Animal.Type.class); for (Animal a : Animal.getAnimals()) { animalTypeMap.put(a.getType(), a); } printResult(animalTypeMap.values()); } private static void printResult(Collection<Animal> values) { System.out.println(values); } private static void printResult(List<Animal>[] animalByType) { for (List<Animal> as : animalByType) { System.out.println(as); } } } class Animal{ public static enum Type{LINGZHANG, CAT, FLY}; private String name; private Type type; public Animal(String name, Type type) { this.name = name; this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Type getType() { return type; } public void setType(Type type) { this.type = type; } @Override public String toString() { return "name:" + name; } public static List<Animal> getAnimals(){ Animal tiger = new Animal("tiger", Animal.Type.CAT); Animal chiken = new Animal("chiken", Animal.Type.LINGZHANG); Animal monkey = new Animal("monkey", Animal.Type.FLY); List<Animal> animals = new ArrayList<Animal>(); animals.add(tiger); animals.add(chiken); animals.add(monkey); return animals; } }
时间: 2024-10-22 22:34:14