模拟枚举类型的类

发布于 2024-09-15 18:20:12 字数 1635 浏览 4 评论 0原文

我怎样才能在java <5.0 ..中模拟枚举类型的类?

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));
    }
}

how can I do to simulate a class of type enum in java <5.0 ..??

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));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁吢 2024-09-22 18:20:12

使用有效的java中描述的类型安全枚举。以下是 Joshua Blochs 文章中给出的示例

// 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");
}

如果您希望类型安全枚举 Serialized< /a>,记住通过 readResolve 方法控制重建。

Use the typesafe enum described in effective java. Here is an example given from 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");
}

If you want your typesafe enum to be Serializable, remember to control reconstruction via the readResolve method.

寄离 2024-09-22 18:20:12

Joshua Bloch 很久以前写了一篇文章对此进行了解释。

Joshua Bloch wrote, long ago, an article explaining it.

浅笑依然 2024-09-22 18:20:12

Joshua Bloch 在他的《Effective Java》第一版中向您展示了如何操作。我要去上班,所以现在无法输入详细信息。更多内容敬请关注。

Joshua Bloch shows you how in the first edition of his "Effective Java". I have to go to work, so I can't type in details now. More to follow.

南渊 2024-09-22 18:20:12

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

The solution Krock and Riduidel are suggesting is pretty neat, but AFAIK a switch-case statement won't be possible as it's applicable only for convertible int values and enum constants.
switch-case combination is a nice feature, more readable than a bunch of ifs (BTW: switch-case will be compiled as a bunch of ifs anyway, correct?). So, to complete the discussed pattern, could someone tell whether there is a quick way to enable this patteren with functionality allowing switch-case? Adding an int property and getter for it seems the simplest way, but is it the optimal?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文