没注意到一直以为java的enum只是简单的数字表示跟C一样,
没想到可以和类一样
转载:http://wosyingjun.iteye.com/blog/1895381
------------------------------------------------------------------------------------------------------
1、enum的遍历和基本方法
package yingjun.enumeration;
enum Shrubbery { GROUND, CRAWLING, HANGING }
public class EnumClass {
public static void main(String[] args) {
//调用enum的value()方法可以遍历enum实例
for(Shrubbery s : Shrubbery.values()) {
System.out.println(s + " ordinal: " + s.ordinal()); //返回enum实例的次序
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(s.name());//获取相应的enum实例 和toString()方法效果相同
System.out.println("----------------------");
}
// 通过字符生成枚举类型
for(String s : "HANGING CRAWLING GROUND".split(" ")) {
Shrubbery shrub = Enum.valueOf(Shrubbery.class, s);
System.out.println(shrub);
}
}
}
GROUND ordinal: 0 -1 false false class yingjun.enumeration.Shrubbery GROUND ---------------------- CRAWLING ordinal: 1 0 true true class yingjun.enumeration.Shrubbery CRAWLING ---------------------- HANGING ordinal: 2 1 false false class yingjun.enumeration.Shrubbery HANGING ---------------------- HANGING CRAWLING GROUND
2、向enum中添加自己的方法
package yingjun.enumeration;
public enum OzWitch {
//构建实例的通同时添加自身的描述
WEST("Miss Gulch, aka the Wicked Witch of the West"),
NORTH("Glinda, the Good Witch of the North"),
EAST("Wicked Witch of the East, wearer of the Ruby " +"Slippers, crushed by Dorothy's house"),
SOUTH("Good by inference, but missing");
private String description;
private OzWitch(String description) {
this.description = description;
}
public String getDescription() { return description; }
public static void main(String[] args) {
for(OzWitch witch : OzWitch.values())
System.out.println(witch + ": " + witch.getDescription());
}
}
WEST: Miss Gulch, aka the Wicked Witch of the West NORTH: Glinda, the Good Witch of the North EAST: Wicked Witch of the East, wearer of the Ruby Slippers, crushed by Dorothy's house SOUTH: Good by inference, but missing
3、重写enum的toString()方法
//重写toString()方法
public enum SpaceShip {
SCOUT, CARGO, TRANSPORT, CRUISER, BATTLESHIP, MOTHERSHIP;
public String toString() {
String newname = name();
String lower = newname.substring(1).toLowerCase();
return newname.charAt(0) + lower;
}
public static void main(String[] args) {
for(SpaceShip s : values()) {
System.out.println(s);
}
}
}
Scout Cargo Transport Cruiser Battleship Mothership
4、switch语句中的enum
package yingjun.enumeration;
enum Signal { GREEN, YELLOW, RED, }
public class TrafficLight {
Signal color = Signal.RED;
public String toString() {
return "The traffic light is " + color;
}
public void change() {
//编译器自动调用ordinal()产生整数的次序
switch(color) {
case RED: color = Signal.GREEN;
break;
case GREEN: color = Signal.YELLOW;
break;
case YELLOW: color = Signal.RED;
break;
}
}
public static void main(String[] args) {
TrafficLight t = new TrafficLight();
for(int i = 0; i < 7; i++) {
System.out.println(t);
t.change();
}
}
}
The traffic light is RED The traffic light is GREEN The traffic light is YELLOW The traffic light is RED The traffic light is GREEN The traffic light is YELLOW The traffic light is RED
5、随机选取enum中的实例
package yingjun.enumeration;
import java.util.*;
public class Enums {
private static Random rand = new Random(47);
public static <T extends Enum<T>> T random(Class<T> ec) {
return random(ec.getEnumConstants());
}
public static <T> T random(T[] values) {
return values[rand.nextInt(values.length)];
}
}
package yingjun.enumeration;
enum Activity { SITTING, LYING, STANDING, HOPPING,
RUNNING, DODGING, JUMPING, FALLING, FLYING }
public class RandomTest {
public static void main(String[] args) {
for(int i = 0; i < 20; i++)
System.out.print(Enums.random(Activity.class) + "\n");
}
}
- STANDING
- FLYING
- RUNNING
- STANDING
- RUNNING
- STANDING
- LYING
- DODGING
- SITTING
- RUNNING
- HOPPING
- HOPPING
- HOPPING
- RUNNING
- STANDING
- LYING
- FALLING
- RUNNING
- FLYING
- LYING