JAVA枚举小结

    最近在项目中看见了一种新的定义常量形式,了解了下JAVA的枚举类。

一、什么是枚举

   枚举(enum)类型是Java5新增的特性,它是一种新的类型,允许用常量表示特定的数据片段,而且全部都已类型安全的形式表示。

二、为什么不用静态变量代替枚举类

    public static final int SEASON_SPRING = 1;
    public static final int SEASON_SUMMER = 2;
    public static final int SEASON_FALL = 3;
    public static final int SEASON_WINTER = 4;

枚举类更加直观,类型安全。使用常量会有以下几个缺陷:

  1. 类型不安全。若一个方法中要求传入季节这个参数,用常量的话,形参就是int类型,开发者传入任意类型的int类型值就行,但是如果是枚举类型的话,就只能传入枚举类中包含的对象。

  2. 没有命名空间。开发者要在命名的时候以SEASON_开头,这样另外一个开发者再看这段代码的时候,才知道这四个常量分别代表季节。

三、枚举类的特性

枚举类更加直观,类型安全。使用常量会有以下几个缺陷:

  1. enum和class、interface的地位一样
  2. 使用enum定义的枚举类默认继承了java.lang.Enum,而不是继承Object类。枚举类可以实现一个或多个接口 不能再继承其他类 因JAVA是单继承的。
  3. 枚举类的所有实例都必须放在第一行展示,不需使用new 关键字,不需显式调用构造器。自动添加public static final修饰。
  4. 使用enum定义、非抽象的枚举类默认使用final修饰,不可以被继承。
  5. 枚举类的构造器只能是私有的。

四、枚举类的使用

定义枚举类:

package com;
 
public enum Color {
     
     RED, GREEN, BLANK, YELLOW 
 
}
使用:
package com;
 
public class B {
 
    public static void main(String[] args) {
 
 
        System.out.println( isRed( Color.BLANK ) ) ;  //结果: false
        System.out.println( isRed( Color.RED ) ) ;    //结果: true
 
    }
 
 
    static boolean isRed( Color color ){
        if ( Color.RED.equals( color )) {
            return true ;
        }
        return false ;
    }
 
}


Switch语句中的使用:

package com;
 
public class B {
 
    public static void main(String[] args) {
 
        showColor( Color.RED );
 
    }
 
    static void showColor(Color color){
        switch ( color ) {
        case BLANK:
            System.out.println( color );
            break;
        case RED :
            System.out.println( color );
            break;
        default:
            System.out.println( color );
            break;
        }
         
    }
}


自定义函数:

package com;
 
public enum Color {
     
     RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);
     
     
    private String name ;
    private int index ;
     
    private Color( String name , int index ){
        this.name = name ;
        this.index = index ;
    }
     
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getIndex() {
        return index;
    }
    public void setIndex(int index) {
        this.index = index;
    }
     
 
}


使用:

package com;
 
public class B {
 
    public static void main(String[] args) {
 
        //输出某一枚举的值
        System.out.println( Color.RED.getName() );
        System.out.println( Color.RED.getIndex() );
 
        //遍历所有的枚举
        for( Color color : Color.values()){
            System.out.println( color + "  name: " + color.getName() + "  index: " + color.getIndex() );
        }
    }
 
}





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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值