枚舉的理論知識:
如果一個對象的所有值可以一一列舉出來,則稱爲枚舉。
在Java中,枚舉類型也是一個類,用枚舉類的變量表示枚舉值。
枚舉類中除了枚舉常量外,還可以定義域、方法、構造方法,main方法也可以定義在枚舉類中。
如果在枚舉類中定義了域和方法,則枚舉常量必須放在枚舉體內的第一行,而且最後一個枚舉常量後要加一個“;”。
code
package com.ls.enumm;
enum Season{
SPRING("春天"){
public String getTitle() {
return "主題是"+this;
}
},
SUMMER("夏天"){
public String getTitle() {
return "主題是"+this;
}
};
private String title;
private Season (String title) {
this.title =title;
}
public String toString () {
return this.title;
}
public abstract String getTitle();
}
public class EnumDemo {
public static void main(String[] args) {
System.out.println(Season.SPRING.getTitle());
}
}
這裏小編提一個問題,enum和Enum有什麼區別呢?