[Java 教學範例拷貝]- 列舉上的方法
剛才找資料時發現一個的Java 教學網站,趕快發揮(C/P)的長才將它備份來,有需要的同好,歡迎來(C/P)一下^^。
拷貝來源:
http://openhome.cc/Gossip/JavaGossip-V1/
http://openhome.cc/Gossip/JavaGossip-V1/AddMethodToEnum.htm
public enum OpConstants { TURN_LEFT, TURN_RIGHT, SHOOT; public String getDescription() { switch(this.ordinal()) { case 0: return "向左轉"; case 1: return "向右轉"; case 2: return "射擊"; default: return null; } } } public class ShowEnum { public static void main(String[] args) { for(OpConstants c : OpConstants.values()) { System.out.printf("%s%n\t%s%n", c, c.getDescription()); } } } public enum OpConstants { TURN_LEFT("向左轉"), TURN_RIGHT("向右轉"), SHOOT("射擊"); private String description; OpConstants(String description) { this.description = description; } public String getDescription() { return description; } } public interface IEnumDescription { public String getDescription(); } public enum OpConstants implements IEnumDescription { TURN_LEFT("向左轉"), TURN_RIGHT("向右轉"), SHOOT("射擊"); private String description; OpConstants(String description) { this.description = description; } public String getDescription() { return description; } }
|
沒有留言:
張貼留言