Java中枚举的命名:单数还是复数?

时间:2023-01-26 13:19:18

Is there an "official" recommendation of how to name Java enums?

是否有关于如何命名Java枚举的“官方”建议?

enum Protocol { HTTP, HTTPS, FTP }

or

enum Protocols { HTTP, HTTPS, FTP }

I know in the .Net world the recommendation is to use singular except for enums that represent bit flags. Just curious if there is something similar in Java.

我知道。net世界的建议是使用单数,除了表示位标志的枚举。只是好奇Java中是否有类似的东西。

A related question that seems to be .Net specific: Singular or plural for enumerations?

一个似乎是。net特有的相关问题:枚举的单数还是复数?

2 个解决方案

#1


169  

Enums in Java (and probably enums in general) should be singular. The thinking is that you're not selecting multiple Protocols, but rather one Protocol of the possible choices in the list of values.

Java中的枚举(通常可能是枚举)应该是单数的。我们的想法是,您选择的不是多个协议,而是值列表中可能选择的一个协议。

Note the absence of plurals: http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

注意没有复数:http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

#2


16  

In the same way that when you are defining a table name in a database or a class in Java you use singular for enums it's also the best option. Just see how you are going to use it.

同样,当您在数据库或Java中的类中定义表名时,您对枚举使用单数也是最好的选择。看看你将如何使用它。

Let's write an example:

让我们写一个例子:

public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}

class Appointment {
private Day day;

public void setDay(Day day) {
    this.day = day;
}
}

In singular form you see clearly the intention of the day attribute. "day" its the day of the week this appointment is going to be held. Otherwise the signature of the method would have been setDay(Days day) and for sure a lot of people will be wondering if the appointment could happen in more than one day.

在单数形式中,您可以清楚地看到day属性的意图。今天是星期几,这个约会就要开始了。否则,该方法的签名将是定日(天),而且可以肯定的是,许多人会想,是否可以在超过一天的时间内进行约会。

If you work in a company the has 48 hour shifts you could try to define something like:

如果你在一家公司工作,工作时间为48小时,你可以试着定义如下:

public enum Days {
MONDAY_TUESDAY, WEDNESDAY_THURSDAY, FRIDAY_SATURDAY
}

That way you could set the days you are going to work. But still it will look weird and there is a better option and is to use the singular form:

这样你就可以设定你上班的时间。但它看起来还是很奇怪,还有更好的选择,就是使用单数形式:

public enum Shift {
MONDAY_TUESDAY, WEDNESDAY_THURSDAY, FRIDAY_SATURDAY
}

Now you really are showing the meaning of the enum. Usually in any domain you are going to find that using singular for an enum is the best option as each constant in the enum is just one element.

现在你真正展示了enum的意义。通常在任何域中,您都将发现为enum使用单数是最好的选择,因为enum中的每个常量都只是一个元素。

You also mention .NET. A "flags" enum in .NET just means that when you are expecting that enum as a parameter what you really get is a list of elements of that enum (stored as a integer).

你也提到。net。. net中的“flags”枚举仅仅意味着当您期望枚举作为一个参数时,您真正得到的是该枚举元素的列表(存储为一个整数)。

// Define an Enum with FlagsAttribute.
[FlagsAttribute] 
enum MultiHue : short
{
    Black = 0,
    Red = 1,
    Green = 2,
    Blue = 4
};

public void setMultiHue(MultiHue hues);

You could do the same in Java, but the enum still will be singular:

在Java中也可以这样做,但是enum仍然是单数的:

public enum Hue {
BLACK, RED, GREEN, BLUE;
private final Integer hue;

Hue() {
    this.hue = 1 << this.ordinal();
}

public Integer toFlag() {
    return this.hue;
}
}

public class MultiHue {
private Integer hues = 0;

public void addHue(Hue hue) {
    this.hues |= hue.toFlag();
}

public boolean hasHue(Hue hue) {
    return (this.hues & hue.toFlag()) != 0;
}
}

An easier and clearer (although it uses a more memory) to do this with Java is just to use a List.

使用Java更容易、更清楚(尽管它使用了更多内存)的方法是使用列表。

#1


169  

Enums in Java (and probably enums in general) should be singular. The thinking is that you're not selecting multiple Protocols, but rather one Protocol of the possible choices in the list of values.

Java中的枚举(通常可能是枚举)应该是单数的。我们的想法是,您选择的不是多个协议,而是值列表中可能选择的一个协议。

Note the absence of plurals: http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

注意没有复数:http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

#2


16  

In the same way that when you are defining a table name in a database or a class in Java you use singular for enums it's also the best option. Just see how you are going to use it.

同样,当您在数据库或Java中的类中定义表名时,您对枚举使用单数也是最好的选择。看看你将如何使用它。

Let's write an example:

让我们写一个例子:

public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}

class Appointment {
private Day day;

public void setDay(Day day) {
    this.day = day;
}
}

In singular form you see clearly the intention of the day attribute. "day" its the day of the week this appointment is going to be held. Otherwise the signature of the method would have been setDay(Days day) and for sure a lot of people will be wondering if the appointment could happen in more than one day.

在单数形式中,您可以清楚地看到day属性的意图。今天是星期几,这个约会就要开始了。否则,该方法的签名将是定日(天),而且可以肯定的是,许多人会想,是否可以在超过一天的时间内进行约会。

If you work in a company the has 48 hour shifts you could try to define something like:

如果你在一家公司工作,工作时间为48小时,你可以试着定义如下:

public enum Days {
MONDAY_TUESDAY, WEDNESDAY_THURSDAY, FRIDAY_SATURDAY
}

That way you could set the days you are going to work. But still it will look weird and there is a better option and is to use the singular form:

这样你就可以设定你上班的时间。但它看起来还是很奇怪,还有更好的选择,就是使用单数形式:

public enum Shift {
MONDAY_TUESDAY, WEDNESDAY_THURSDAY, FRIDAY_SATURDAY
}

Now you really are showing the meaning of the enum. Usually in any domain you are going to find that using singular for an enum is the best option as each constant in the enum is just one element.

现在你真正展示了enum的意义。通常在任何域中,您都将发现为enum使用单数是最好的选择,因为enum中的每个常量都只是一个元素。

You also mention .NET. A "flags" enum in .NET just means that when you are expecting that enum as a parameter what you really get is a list of elements of that enum (stored as a integer).

你也提到。net。. net中的“flags”枚举仅仅意味着当您期望枚举作为一个参数时,您真正得到的是该枚举元素的列表(存储为一个整数)。

// Define an Enum with FlagsAttribute.
[FlagsAttribute] 
enum MultiHue : short
{
    Black = 0,
    Red = 1,
    Green = 2,
    Blue = 4
};

public void setMultiHue(MultiHue hues);

You could do the same in Java, but the enum still will be singular:

在Java中也可以这样做,但是enum仍然是单数的:

public enum Hue {
BLACK, RED, GREEN, BLUE;
private final Integer hue;

Hue() {
    this.hue = 1 << this.ordinal();
}

public Integer toFlag() {
    return this.hue;
}
}

public class MultiHue {
private Integer hues = 0;

public void addHue(Hue hue) {
    this.hues |= hue.toFlag();
}

public boolean hasHue(Hue hue) {
    return (this.hues & hue.toFlag()) != 0;
}
}

An easier and clearer (although it uses a more memory) to do this with Java is just to use a List.

使用Java更容易、更清楚(尽管它使用了更多内存)的方法是使用列表。