enum定義的xxxx實際上是個類別
2.為何要有enum?
在interface中,可以定義常數。例如:
2.為何要有enum?
在interface中,可以定義常數。例如:
public interface Action { public static final int STOP = 0; public static final int RIGHT = 1; public static final int LEFT = 2; public static final int UP = 3; public static final int DOWN = 4; }但後面在取用時只能透過Action.STOP這種方式取,但其實不易讀 之後發展出enum,使用起來更為方便
3.實際應用
例子1
public class test { //設定建構子 public enum Level { DOWNLOAD_QUANTITY(0, "AA"), DOWNLOAD_CONTENT(1, "BB"), KEYWORD_LENGTH(2, "CC"), STANDARD_PLATFORM_LOGIN(3, "登入"), EDUCATION_VIEW(4, "DD"); private int mode; private String Name; private Level(int mode, String Name) { this.mode = mode; this.Name = Name; } } //執行程式(提示:Level.STANDARD_PLATFORM_LOGIN 等於把STANDARD_PLATFORM_LOGIN常數的值塞入建構子內) public static void main(String[] args) { int n = Level.STANDARD_PLATFORM_LOGIN.mode; String s = Level.STANDARD_PLATFORM_LOGIN.Name; System.out.println(n); //3 System.out.println(s); //登入 } }例子2
沒有留言:
張貼留言