枚举



枚举
枚举就是产生一个新的对象 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' ));
    }
    
}

在线工具 由 开源中国 所有 | @新浪微博 |阿里云提供服务器和

在线工具 由 开源中国 所有 | @新浪微博 |阿里云提供服务器和带宽 |

枚举
枚举就是产生一个新的对象 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' ));
    }
    
}

在线工具 由 开源中国 所有 | @新浪微博 |阿里云提供服务器和

在线工具 由 开源中国 所有 | @新浪微博 |阿里云提供服务器和带宽 |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值