Java 枚举最佳实践
这似乎是一个微不足道的问题,但我对枚举的思考有点混乱。
所以我有一个类 - 假设它称为 DVDPlayer - 我想要一个枚举来表示它是打开、关闭还是支持。
所以我可以将枚举放在类中 - 它在类之外没有意义。我的问题是 - 枚举应该是公共的,以便其他类可以查询值,还是应该将其设为私有,然后使用“isOn”、“isOFf”和“isStandby”方法?
后者听起来有点愚蠢,但我不确定将枚举也公开是否是一个好主意。
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.
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我想说,将其公开似乎是个好主意。主要原因是应用程序更容易扩展,因为您不必在每次添加状态时考虑添加新方法。
如果您决定将其公开,您应该考虑将其作为顶级枚举。我真的不明白你为什么说“这在课堂之外没有意义”。我认为 DVDPlayerState 听起来像是一个完美的公共/顶级枚举。
I would say it seems like a good idea to make it public. Main reason being that the application is easier to extend since 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
DVDPlayerState
sounds like a perfectly fine public / top-level enum.这取决于您想如何使用外部世界的
DVDPlayer
类:或者
我认为第一个是更好的选择。您不必使用委托方法来污染您的代码。
It depends on how you want to use the
DVDPlayer
class from the outside world:or
I think the first one is a better option. You don't have to pollute your code with delegating methods.
根据经验,您希望尽可能保持私密性(尽管通常情况下,这不是枚举的用例),但从您提出问题的方式来看,我不确定您是否按预期使用枚举。
您想使用枚举来表示固定值;与将这些值保存为静态最终整数或字符串相比,它是更干净的替代方案。因此,对于声明为 Your class 的枚举,
它看起来有点像这样:
调用类将使用以下代码:
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
Your class would look a bit like this:
And a calling class would use the following code:
公开枚举可能是有意义的。然后,您将得到如下结果:
如果您只有三种状态,则最好使用 isOn、isOff 和 isStandby 方法。对于更多的州来说,公共枚举更好。另外,enum 可以用在 switch 语句中,这很方便。
Making the enum public could make sense. You would then have something like this:
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.
如果
enum
是公共接口的一部分,则将其声明为public是有意义的。 DVPlayer似乎就是这种情况,因为你说它可以查询。 “isOn”、“isOFf”和“isStandby”这三个方法不必要地使公共接口变得臃肿。但有时
枚举
在类中使用时会派上用场,在这种情况下,应该将其声明为私有。例如,如果 Format 在 DVDPlayer 类内部使用,但不是公共接口的一部分,或者作为参数或返回值,则以下声明是可以的方法。
If the
enum
is 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.But there are times when an
enum
comes in handy for use within the class, in which case it should be declared private. For example, the following declarationwould be okay, if Format is used internally in the DVDPlayer class but is not part of the public interface either as a parameter or return value of a method.
我想我将是第一个在这里提倡使用方法的人。首先,考虑一下您班级的用户。他们需要了解的关于您的业务领域的信息越少越好,因此不要将他们与“状态”之类的东西混淆。在现实生活中,您不会“设置 DVD 播放器的状态”,而只是“打开它”或“如果关闭则关闭”,这对我来说非常需要方法。除此之外,Java 因其严格静态类型而闻名并受到喜爱,它允许您仅通过 IDE 的自动完成建议来发现接口。读取方法
turnOff
对我来说立即有意义,而setState
则不明确,需要我首先查找状态枚举,以便我知道实际状态是什么可以。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
turnOff
makes sense instantly to me, while asetState
is ambiguous and would require me to look up the state enum first in order for me to know what the state actually can be.