【问题标题】:Simulate a class of type enum模拟一类枚举类型
【发布时间】:2010-08-19 09:37:45
【问题描述】:

如何在java

public final class Week {

    private static final Day[] _week = {Day.MONDAY, Day.TUESDAY, Day.WEDNESDAY, Day.THURSDAY, Day.FRIDAY, Day.SATURDAY, Day.SUNDAY};

    public static Day getDayOfWeek(final int index) {
        if (index >= 1 && index <= 7) {
            return _week[index - 1];
        }
        throw new IndexOutOfBoundsException("Invalid index [1..7]");
    }

    public static final class Day {
        public static final Day MONDAY = new Day(1, "Monday");
        public static final Day TUESDAY = new Day(2, "Tuesday");
        public static final Day WEDNESDAY = new Day(3, "Wednesday");
        public static final Day THURSDAY = new Day(4, "Thursday");
        public static final Day FRIDAY = new Day(5, "Friday");
        public static final Day SATURDAY = new Day(6, "Saturday");
        public static final Day SUNDAY = new Day(7, "Sunday");

        private int _value;
        private String _name;

        private Day(final int value, final String name) {
            _value = value;
            _name = name;
        }

        /* (non-Javadoc)
         * @see java.lang.Object#toString()
         */
        public String toString() {
            return "[Day] " + _name;
        }

        /* (non-Javadoc)
         * @see java.lang.String#equals(java.lang.Object)
         */
        public boolean equals(Object anObject) {
            Day d = (Day)anObject;
            return _value == d._value;
        }
    }

    public static void main(String[] agrs) {
         System.out.println(Week.getDayOfWeek(2));
    }
}

【问题讨论】:

    标签: java class enums simulate


    【解决方案1】:

    使用有效的java中描述的类型安全枚举。这是Joshua Blochs article on this给出的一个例子:

    // The typesafe enum pattern
    public class Suit {
        private final String name;
    
        private Suit(String name) { this.name = name; }
    
        public String toString()  { return name; }
    
        public static final Suit CLUBS =
            new Suit("clubs");
        public static final Suit DIAMONDS =
            new Suit("diamonds");
        public static final Suit HEARTS =
            new Suit("hearts");
        public static final Suit SPADES =
            new Suit("spades");
    }
    

    如果您希望您的类型安全枚举为 Serializable,请记住通过 readResolve 方法控制重构。

    【讨论】:

    • 添加equals和hashCode支持:)
    • 谢谢,它是如何工作的。如果我做 thos Suit("spades") 我会得到什么..??
    • @helios,当像这样控制构造时,equals 方法等同于使用==,因为每个不同的 Suit 只有一个实例。
    • @Mercer 您可以通过调用Suit.CLUBSSuit.SPADES 来使用它,就像Suit 是Java 5 枚举一样。如果您想从字符串中解析Suit,那么您需要提供另一种方法,将字符串与每个西装名称进行比较,以查看哪个匹配。
    【解决方案2】:

    Joshua Bloch 很久以前写道,an article 对此进行了解释。

    【讨论】:

      【解决方案3】:

      Joshua Bloch 在他的“Effective Java”的第一版中向您展示了如何做到这一点。我要上班,所以我现在不能输入详细信息。更多内容。

      【讨论】:

      • 事实发生七年后,现在这被否决了?版主,请注意。
      【解决方案4】:

      Krock 和 Riduidel 建议的解决方案非常简洁,但 AFAIK 不可能使用 switch-case 语句,因为它仅适用于可转换的 int 值和 enum 常量。 switch-case 组合是一个不错的功能,比一堆 ifs 更具可读性(顺便说一句:switch-case 无论如何都会被编译为一堆 ifs,对吗?)。那么,为了完成所讨论的模式,有人可以告诉是否有一种快速的方法来启用这个模式,其功能允许switch-case?添加int 属性和getter 似乎是最简单的方法,但它是最优的吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-16
        • 2018-10-18
        • 2017-01-14
        相关资源
        最近更新 更多