枚举类型中的构造方法

package 枚举类型与泛型;
/*
 * 枚举类型中的构造方法
 * 在枚举类型中,可以定义构造方法,但是规定这个构造方法必须为private修饰符所修饰。
 *
 */
public class EnumIndex_Test {
    public enum Constants2{//将常量放置在枚举类型中
        //定义带参数的枚举类型成员
        Constants_A("我是枚举成员A"),
        Constants_B("我是枚举成员B"),
        Constants_C("我是枚举成员C"),
        Constants_D(3);
        private String description;
        private int i=4;
        
        private Constants2() {
            
        }
        private Constants2(String description) {
            this.description=description;
        }
        private Constants2(int i) {
            this.i=this.i+i;
        }
        public String getDescription() {
            return description;
        }
        public int getI() {
            return i;
        }
    }    
    public static void main(String[] args) {
        for(int i=0;i<Constants2.values().length;i++) {
            System.out.println(Constants2.values()[i]+"调用getDescription()方法为:"+Constants2.values()[i].getDescription());
        }
        System.out.println(Constants2.valueOf("Constants_D")+"调用getI()方法为:"+Constants2.valueOf("Constants_D").getI());
    }

}

运行结果:

Constants_A调用getDescription()方法为:我是枚举成员A
Constants_B调用getDescription()方法为:我是枚举成员B
Constants_C调用getDescription()方法为:我是枚举成员C
Constants_D调用getDescription()方法为:null
Constants_D调用getI()方法为:7


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值