【发布时间】:2011-04-23 19:50:15
【问题描述】:
我已经阅读过 Java 枚举并经常使用它们。但是,我不明白为什么 JFrame.EXIT_ON_CLOSE 会返回 int。
考虑http://download.oracle.com/javase/1.5.0/docs/guide/language/enums.html;
// int Enum Pattern - has severe problems!
public static final int SEASON_WINTER = 0;
public static final int SEASON_SPRING = 1;
public static final int SEASON_SUMMER = 2;
public static final int SEASON_FALL = 3;
不是类型安全的 - 由于季节只是一个 int,因此您可以在需要季节的任何其他 int 值中传递,或者将两个季节相加(这没有意义)。
JFrame.EXIT_ON_CLOSE返回3,而JFrame.HIDE_ON_CLOSE返回1,这意味着后者的三个等于第一个。
为什么要这样实现?
【问题讨论】: