在线工具 由 开源中国 所有 | @新浪微博 |阿里云提供服务器和
在线工具 由 开源中国 所有 | @新浪微博 |阿里云提供服务器和带宽 |
枚举 枚举就是产生一个新的对象 Grade A =new Grade("100-90") 然后获得其属性 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 class Grade{ private Grade(){} public static Grade A = new Grade(); public static Grade B = new Grade(); public static Grade C = new Grade(); public static Grade D = new Grade(); public static Grade E = new Grade(); enum Grade{ //与上面的代码功能完全一样 A( "100~90" ){ public String toLocaleString(){ //重写抽象方法 return "优秀" ; } },B( "89~80" ){ public String toLocaleString(){ return "良好" ; } },C( "79~70" ){ public String toLocaleString(){ return "一般" ; } },D( "69~60" ){ public String toLocaleString(){ return "刚及格" ; } },E( "59~0" ){ public String toLocaleString(){ return "不及格" ; } }; //代表着自身的一个个的实例对象 // 字段 private String value; // A 100~90 B 89~80 C 79~70 D 69~60 E 59~0 //构造函数, private Grade(){} //枚举的构造方法一定是私有的 private Grade(String value){ this .value = value; } public String getValue() { return value; } public abstract String toLocaleString(); //使用抽象方法获得 public static String getValue(Character ch){ //使用静态方法 switch (ch) { case 'A' : return "优秀" ; case 'B' : return "良好" ; } return "没戏" ; } } 1 2 3 4 5 6 7 8 9 10 11 public class Test1 { @Test public void student(){ Grade grade= Grade.A;//通过 String a=grade.getValue(); System.out.println(a); System.out.println(grade.toLocaleString()); System.out.println( "学生水平" +Grade.getValue( 'A' )); } }在线工具 由 开源中国 所有 | @新浪微博 |阿里云提供服务器和
在线工具 由 开源中国 所有 | @新浪微博 |阿里云提供服务器和带宽 |
