关于枚举类的构造方法

xiaoxiao2021-02-28  79

输出结果: It is a account type It is a account type It is a account type SAVING 解释:简而言之,枚举类有是三个实例,故调用三次构造方法,打印三次It is a account type 2. public class EnumIndexTest { 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
转载请注明原文地址: https://www.6miu.com/read-53979.html

最新回复(0)