类型安全枚举

  public class Suit {
    private static int nextOrdinal = 0;
    private final int ordinal = nextOrdinal++;
    private final String name;
    private Suit (String name) {
       this.name = name;
    }
    public String toString() {
       return this.name;
    }
    public int ordinal() {
       return this.ordinal;
    }
    public static final Suit CLUBS = new Suit("clubs");
    public static final Suit DIAMONDS = new Suit("diamonds");
    public static final Suit HEARTS= new Suit("hearts");
    public static final Suit SPADES= new Suit("spades");
    
  }

你可能感兴趣的:(安全)