枚举 enum
获取枚举,直接以 枚举类.值来获取
枚举的本质就是一个java类,构造方法必须是私有
枚举的值就是类的实例对象
例:
/**
* 用于表示卡牌类型的枚举
* @author Administrator
*
*/
public enum CardType {
//枚举的值就是类的实例对象
//黑桃
SPADE("黑桃"),
//红桃
HEART("红桃"),
//梅花
CLUB("梅花"),
//方块
DIAMOND("方块");
private String type;
/**
* 枚举的本质就是一个Java类,构造方法必须是私有
* @param type
*/
private CardType(String type){
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
/**
* 扑克牌类
* @author Administrator
*
*/
public class Card {
//花色
private CardType type;
//牌面值
private int number;
public Card(CardType type, int number) {
super();
this.type = type;
this.number = number;
}
public CardType getType() {
return type;
}
public void setType(CardType type) {
this.type = type;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String toString(){
return "这是一张"+type.getType()+" "+number;
}
public static void main(String[] args) {
//构建卡牌
//获取枚举直接枚举类.值来获取
Card card = new Card(CardType.DIAMOND, 10);
System.out.println(card);
}
}