作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
class Ideone
{
enum Members{
A(1),B(2),C(3); // I have 3 enum constants here
private int size;
Members(int size){
//System.out.println("Initializing var with size = "+size);
}
}
public static void main (String[] args) throws java.lang.Exception
{
System.out.print("main ");
// your code goes here
for (Members i: Members.values()){
System.out.print(i.name());
System.out.println(i.size);
}
}
}
这是程序的输出-
主要A0
乙0
C0
为什么尺寸值不打印为 1、2 和 3,而是打印为 0?
最佳答案
因为你没有映射size属性。将枚举构造函数更改为:
Members(int size){
this.size = size;
}
顺便说一句:您应该始终将枚举字段标记为final
,因为枚举只存在一次(单例)。因此将 private int size
更改为 private Final int size
关于java - 你能解释一下这个使用枚举的java程序的输出吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26324931/
我是一名优秀的程序员,十分优秀!