java.Lang.Enumの基本特性

5570 ワード

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) + "
"); } }

 
STANDING
FLYING
RUNNING
STANDING
RUNNING
STANDING
LYING
DODGING
SITTING
RUNNING
HOPPING
HOPPING
HOPPING
RUNNING
STANDING
LYING
FALLING
RUNNING
FLYING
LYING