Java 枚举最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3582430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java enum best practice
提问by pecks
This might seem like a trivial question, but I'm a bit muddled in my thinking regarding enums..
这似乎是一个微不足道的问题,但我对枚举的想法有点混乱..
So I have a class - let's say its called DVDPlayer - and I want to have an enum that represents whether it's ON, OFF, or STANDBY.
所以我有一个类 - 假设它叫做 DVDPlayer - 我想要一个枚举来表示它是打开、关闭还是待机。
So I can put the enum in the class - it doesn't make sense outside of the class. My question is this - should the enum be public, so that other classes can query values, or should I make it private, and then have "isOn", "isOFf" and "isStandby" methods?
所以我可以把枚举放在课堂上——它在课堂之外没有意义。我的问题是 - 枚举应该是公共的,以便其他类可以查询值,还是应该将其设为私有,然后使用“isOn”、“isOFF”和“isStandby”方法?
The latter sounds a bit daft, but I'm unsure as to whether it's a good idea to have the enum as public as well.
后者听起来有点愚蠢,但我不确定将枚举也公开是否是一个好主意。
采纳答案by aioobe
I would say it seems like a good idea to make it public. Main reason being that the application is easier to extendsince you wouldn't have to think about adding new methods each time you add a state.
我想说把它公之于众似乎是个好主意。主要原因是应用程序更容易扩展,因为每次添加状态时您都不必考虑添加新方法。
If you decide to make it public, you should consider having it as top-level enum. I don't really see why you say "it doesn't make sense outside of the class". I think a DVDPlayerStatesounds like a perfectly fine public / top-level enum.
如果您决定将其公开,则应考虑将其作为顶级枚举。我真的不明白你为什么说“在课堂之外没有意义”。我认为 aDVDPlayerState听起来像是一个完美的公共/顶级枚举。
回答by Petar Minchev
It depends on how you want to use the DVDPlayerclass from the outside world:
这取决于您想如何使用DVDPlayer外部世界的类:
if (dvdPlayer.getState() == State.ON)
or
或者
if (dvdPlayer.isOn())
I think the first one is a better option. You don't have to pollute your code with delegating methods.
我认为第一个是更好的选择。您不必使用委托方法污染您的代码。
回答by mikek
As a rule of thumb you want to keep things as private as possible (though usually, that's not the use case with enums), but from the way your question is posed, I'm not sure you're using enums as intended.
根据经验,您希望尽可能保持私密性(尽管通常情况下,这不是枚举的用例),但是从您提出问题的方式来看,我不确定您是否按预期使用了枚举。
You want to use an enum to represent fixed values; it's cleaner alternative to saving these values as static final Integers or Strings. So for an enum declared as
您想使用枚举来表示固定值;将这些值保存为静态最终整数或字符串是更干净的替代方法。所以对于声明为的枚举
public enum DvdState { ON, OFF, STANDBY };
Your class would look a bit like this:
你的类看起来有点像这样:
public class DvdPlayer {
private DvdState state = DvdState.OFF;
public void setState(DvdState state) {
this.state = state;
}
}
And a calling class would use the following code:
调用类将使用以下代码:
dvdPlayer.setState(DvdState.ON);
回答by Jimmy Isaac
If the enumis part of the public interface, it makes sense to declare it public. That seems to be the case with the DVPlayer, because you say it can be queried. The three methods "isOn", "isOFf" and "isStandby" unnecessarily bloat the public interface.
如果enum是公共接口的一部分,则将其声明为public是有意义的。DVPlayer似乎就是这种情况,因为您说它可以查询。三个方法“ isOn”、“ isOFF”和“ isStandby”不必要地使公共接口膨胀。
But there are times when an enumcomes in handy for use within the class, in which case it should be declared private. For example, the following declaration
但有时 anenum在类中使用时会派上用场,在这种情况下,应将其声明为private。例如,下面的声明
private enum Format{DVD, BLURAY};
would be okay, if Formatis used internally in the DVDPlayerclass but is not part of the public interface either as a parameter or return value of a method.
如果Format在DVDPlayer类内部使用,但不是作为参数或方法的返回值的公共接口的一部分,那就没问题了。
回答by Hubert Grzeskowiak
I guess I'm gonna be the first one to advocate the use of methods here. First, think about the users of your class. The less they need to know about your business domain, the better, so don't confuse them with things like "state". You wouldn't "set the state" of a DVD player in real life, but just "turn it on" or "turn if off", which to me screams for methods. Aside from that Java is known and loved for being strictly statically typed, which allows you to discover an interface solely by the auto-complete suggestions of your IDE. Reading a method turnOffmakes sense instantly to me, while a setStateis ambiguous and would require me to look up the state enum first in order for me to know what the state actually can be.
我想我会是第一个在这里提倡使用方法的人。首先,想想你班级的用户。他们对您的业务领域的了解越少越好,所以不要将他们与“状态”之类的东西混淆。在现实生活中,您不会“设置”DVD 播放器的状态,而只是“打开它”或“关闭时关闭”,这对我来说是一种方法。除此之外,Java 以严格的静态类型而闻名和喜爱,它允许您仅通过 IDE 的自动完成建议来发现接口。阅读一个方法turnOff对我来说很有意义,而 asetState是模棱两可的,需要我首先查找状态枚举,以便我知道状态实际上是什么。
回答by kgiannakakis
Making the enum public could make sense. You would then have something like this:
公开枚举可能是有意义的。然后你会有这样的事情:
DvdPlayer.State getState();
If you only have three states, it may be preferable to use the isOn, isOff, and isStandby methods. For more states the public enum is better. Also an enum can be used in a switch statement, which is convenient.
如果只有三种状态,最好使用 isOn、isOff 和 isStandby 方法。对于更多的州,公共枚举更好。也可以在 switch 语句中使用枚举,这很方便。

