哪里使用枚举类型

发布于 2024-11-28 05:20:13 字数 211 浏览 2 评论 0原文

我必须创建自己的日历类(我知道已经存在一个,但我们需要编写自己的日历类,因此我无法使用预先存在的 CalendarGregorianCalendar 中的任何方法 类),并且我们需要使用枚举类型。我不确定我会用它们做什么,因为我对它们不太熟悉。

如果有帮助,我的构造函数会采用日期、月份和年份值(例如 2011 年 5 月 8 日)。

I have to create my own calendar class (I know one already exists, but we are required to write our own, so I cant use any of the methods from the pre-existing Calendar or GregorianCalendar classes), and we are required to use enum types. I'm not sure what I would use these for, as I am not too familiar with them.

If it helps, my constructors take a date, month, and year value (e.g. 5, 8, 2011).

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

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

发布评论

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

评论(2

心奴独伤 2024-12-05 05:20:13

枚举是处理相同类型的有限值集时的首选武器。例如:

public enum Month {
    JANUARY,
    FEBRUARY,
    MARCH,
    APRIL,
    ... etc
}

这使得它们的使用强类型

您可以指定接受枚举作为参数的方法,例如:

private Month month;
public void setMonth(Month month) {
    this.month = month;
}

另一种方法是使用字符串(或整数)常量,通常称为字符串类型,但此类常量不是类型化的 - 接受字符串的方法将接受 >any 字符串 - 例如“Foo”:

private String month;
public void setMonth(String month) {
    this.month = month; // Will accept "Foo"
}

枚举可以(大部分)像普通类一样使用 - 你可以有构造函数(私有)、字段和方法,如下所示:

public enum Month {
    JANUARY("January",
    FEBRUARY("February",
    ... 
    DECEMBER("December");

    private final name;

    private Month(String name) { // Constructors must be private
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

你甚至可以有设置器,因为枚举不必须是不可变(它们的字段不必是最终的),但不建议这样做。

编辑:更多信息

枚举不能扩展,但它们可以实现,因此如果您有多个月份类型(针对不同的日历),您可以这样做以保持强类型。这是日历和月份的抽象示例

interface Month {
}

enum GregorianMonth implements Month {
}

enum ChineseMonth implements Month {
}

abstract class Calendar<T extends Enum<T> & Month> { // T must be both an enum and a Month
    T month;

    void setMonth(T month) {
        this.month = month;
    }
}

class GregorianCalendar extends Calendar<GregorianMonth> {

}

class ChineseCalendar extends Calendar<ChineseMonth> {

}

enums are the weapon of choice when dealing with a finite set of values of the same type. For example:

public enum Month {
    JANUARY,
    FEBRUARY,
    MARCH,
    APRIL,
    ... etc
}

This makes the use of them strongly typed.

You can specify methods to accept enum as parameters, eg:

private Month month;
public void setMonth(Month month) {
    this.month = month;
}

The alternative is to use String (or int) contants, often called stringly typed, but such constants are not typed - methods accepting Strings will accept any String - eg "Foo":

private String month;
public void setMonth(String month) {
    this.month = month; // Will accept "Foo"
}

enum can be used (mostly) like normal classes - you can have constructors (private), fields and methods, like this:

public enum Month {
    JANUARY("January",
    FEBRUARY("February",
    ... 
    DECEMBER("December");

    private final name;

    private Month(String name) { // Constructors must be private
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

You can even have setters, because enum are do not have to be immutable (their fields do not have to be final), but it's not advised.

EDITED: More info

enums can not extends but they can implements, so if you had multiple Month types (for different Calendars) you could do this to keep strong typing. Here's and example of an abstraction of Calendar and Month

interface Month {
}

enum GregorianMonth implements Month {
}

enum ChineseMonth implements Month {
}

abstract class Calendar<T extends Enum<T> & Month> { // T must be both an enum and a Month
    T month;

    void setMonth(T month) {
        this.month = month;
    }
}

class GregorianCalendar extends Calendar<GregorianMonth> {

}

class ChineseCalendar extends Calendar<ChineseMonth> {

}
儭儭莪哋寶赑 2024-12-05 05:20:13

您的第一站应该是 Java enum 教程,我认为很好地解释了它们是什么以及何时需要使用它们。

除此之外,简短的答案是,您可以将它们用于任何值可以是特定(有限)数量的常量之一的情况。我确信您可以想到至少一个与日历相关的字段,该字段被限制在一定范围的有效值(尽管您不允许使用内置日历,但如果您想要一些日历,请看一下它们)提示)。

Your first stop should be the Java enum tutorial, which I think does a good job of explaining what they are, and when you'd want to use them.

The short answer beyond that is that you'd use them for anything where a value can be one of a certain (finite) number of constants. I'm sure you can think of at least one calendar-related field which is constrained to a certain range of valid values (and though you're not allowed to use the built-in calendars, take a look at them if you want some hints).

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