think in java -Enum

#

enum Shrubbery {
    GROUND, CRAWLING, HANGING
}
	
public static void main(String[] args) {
    for (Shrubbery s : Shrubbery.values()) {
        System.out.println(s + ", ordinal: " + s.ordinal());
        System.out.println(s.compareTo(Shrubbery.CRAWLING));
        System.out.println(s.equals(Shrubbery.CRAWLING));
        System.out.println(s == Shrubbery.CRAWLING);
        System.out.println(s.getDeclaringClass());
        System.out.println("name: " + s.name());
    }
}

- ordinal(): return an int indicating the declaration order of this enum item, starts from 0.

- we can compare enum using '==', equals() and hashCode() are automatically generated for us.


# Enum

- when we create a new enum type, it will automatically extends Enum class.

- Enum class is Comparable and Serialzable.


# can constructor of enum type be public?

- NO, it should be private or default, public or protected are not allowed.

- complier won't let us to create enum item using constructor.


# how to customize the way of displaying enum item?

- default toString() in Enum class will just return name, we can override toString() to define new format.


# why enum can be used in switch statement?

- Ordinarily, switch statement only accept integral value, BUT enum has ordinal() which will produce the integral order, so complier will do sth like using ordinal() method.


# mystry of values()

- actually, values() and valueOf() are added to enum type by complier.


# can we extends Enum class explicitly? or extends other enum types?

- enum can't extends anything explicitly, because it already extends Enum && java won't allow multi-inheritance

- Also NO for enum types, because complier will make them 'final'.

- BTW, all enum items will be declared as public static final


?? singleton design pattern qeustion??

i've been told that the best practice to write Singleton is using enum, according to the above, the reason maybe all enum items are 'static final', so I'm wondering if we can write Singleton using interface like below:

interface ISingleton {
    ISingleton instance = new ISingleton() {
        @Override
        public void me() {
            System.out.println("am i singleton??");
        }
    };
    void me();
}


# how we get enum instances via Enum class?

- if we upcast our enum type to Enum class, there is no method like values() there, 

BUT, we can use getEnumConstants() like below:

Enum e = myEnum.INSTANCE;
for (Enum en : e.getClass().getEnumConstants()) {
    System.out.println(en);
}


# enum type can implements one or more interfaces.


# enum of enums

1. interface way

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 {
		TIRAMISU, GELATO, BLACK_FOREST_CAKE, FRUIT, CREME_CARAMEL;
	}

	enum Coffee implements Food {
		BLACK_COFFEE, DECAF_COFFEE, ESPRESSO, LATTE, CAPPUCCINO, TEA, HERB_TEA;
	}
}


2. enum way

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) {
		values = kind.getEnumConstants();
	}

	public Food randomSelection() {
		return Enums.random(values);
	}
}


# EnumSet

class EnumSetTest {
	
	enum Color {
		RED, BLUE, BLACK, WHITE
	}
	
	public static void main(String[] args) {
		EnumSet<Color> eSet = EnumSet.noneOf(Color.class);
		print(eSet);
		
		eSet.addAll(EnumSet.allOf(Color.class));
		print(eSet);
		
		eSet.removeAll(EnumSet.range(Color.RED, Color.BLACK));
		print(eSet);
	}
}


# EnumMap

key is enum

public class EnumMaps {
	public static void main(String[] args) {
		EnumMap<AlarmPoints, Command> em = new EnumMap<AlarmPoints, Command>(AlarmPoints.class);
		
		em.put(KITCHEN, new Command() {
			public void action() {
				print("Kitchen fire!");
			}
		});
		em.put(BATHROOM, new Command() {
			public void action() {
				print("Bathroom alert!");
			}
		});
		
		
		for (Map.Entry<AlarmPoints, Command> e : em.entrySet()) {
			printnb(e.getKey() + ": ");
			e.getValue().action();
		}
		try { // If there's no value for a particular key:
			em.get(UTILITY).action();
		} catch (Exception e) {
			print(e);
		}
	}
}

for some cases, we can put enumSet and enumMap inside the enumType to manage all items.


转载于:https://my.oschina.net/u/1242229/blog/287274

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值