标签:
Enum
public enum Fruit { APPLE, BANANA, ORANGE, WATERMELON }
public class EnumTest { public static void main(String[] args) { for(Fruit fruit : Fruit.values()) { test(fruit); } } public static void test(Fruit fruit) { switch (fruit) { case APPLE: System.out.println("This is an apple"); break; case BANANA: System.out.println("This is a banana"); break; case ORANGE: System.out.println("This is an orange"); break; case WATERMELON: System.out.println("This is a watermelon"); break; default: break; } } }
这里的switch也可以用if-else用来代替, 但是其两者在效率上是有区别的
标签:
原文地址:http://www.cnblogs.com/jin12/p/5527977.html