Java:`enum` vs `String` 作为参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/277364/
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` vs `String` as Parameters
提问by Ande Turner
I've been reading through the details of the Systemlibraries setand getmethods yet the parameters are usually Strings.
我一直在阅读System库set和get方法的详细信息,但参数通常是字符串。
Would you consider the use of Stringas parameters bad practise since the inclusion of enum?
String自从包含以来,您会考虑使用作为参数的不良做法enum吗?
A better alternative at minimum might be public final String, No?
一个更好的选择至少可能是public final String,不是吗?
回答by Frank Grimm
I would consider Enums to be a better approach than Strings. They are type safe and comparing them is faster than comparing Strings.
我认为枚举是比字符串更好的方法。它们是类型安全的,比较它们比比较字符串更快。
As a pre Java 1.5 alternative you could use the type-safe enum pattern suggested by Joshua Bloch in his book Effective Java. For type-safe enums see also http://www.javacamp.org/designPattern/enum.html
作为 Java 1.5 之前的替代方案,您可以使用 Joshua Bloch 在他的 Effective Java 一书中建议的类型安全枚举模式。对于类型安全的枚举,另见http://www.javacamp.org/designPattern/enum.html
回答by akuhn
If your set of parameters is limited and known at compile time, use enum.
如果您的参数集在编译时有限且已知,请使用enum.
If your set of parameters is open and unkown at compile time, use strings.
如果您的参数集在编译时是开放的且未知,请使用字符串。
回答by Martin
Just because you declare a public final Stringas something you expect to have passed into a method as a parameter, there's nothing stopping me from passing in anything I like.
仅仅因为你将 a 声明public final String为你希望作为参数传递给方法的东西,没有什么能阻止我传递我喜欢的任何东西。
Using enums means that I can't create my own object to pass in, protecting both sides of the issue. The only time I can think you should be using constant Strings in lieu of enums is if you need to allow room for the user to extend your method to enable custom functionality...
使用enums 意味着我无法创建自己的对象来传入,从而保护问题的双方。我认为您应该使用常量字符串代替枚举的唯一一次是,如果您需要为用户留出空间来扩展您的方法以启用自定义功能......
回答by lycono
If you're referring to System.setProperty(), System.getProperty(), or System.getenv(), I think Strings are appropriate here since the set of possible keys is open. The key parameter corresponds to an actual text/string type value in some file or store somewhere.
如果您指的是 System.setProperty()、System.getProperty() 或 System.getenv(),我认为这里使用字符串是合适的,因为可能的键集是开放的。key 参数对应于某个文件或某处存储中的实际文本/字符串类型值。
If you have a closed set of keys, I think enums would be much preferred.
如果您有一组封闭的键,我认为枚举会更受欢迎。
回答by Vanuan
Although using enums is type safe, the need of converting enum to string and vice versa is quite frequent. And there is no built-in feature to do that in Java. And you'll eventually end up with using valueOf() and toString(). And using that approach will be no much different from using just strings. Because you'll need to handle situations where string can not be converted to Enum.
尽管使用枚举是类型安全的,但将枚举转换为字符串(反之亦然)的需求非常频繁。并且在 Java 中没有内置功能可以做到这一点。你最终会使用 valueOf() 和 toString()。使用这种方法与仅使用字符串没有太大区别。因为您需要处理无法将字符串转换为枚举的情况。
So just using static final strings is easy and is a common practice, AFAIK.
所以只使用静态最终字符串很容易,而且是一种常见的做法,AFAIK。
For example, you'll want to interact with a server using some API. You'll need to define every method and a response as Enum. And then you'll need to add toString and valueOf methods. Why just not use String?
例如,您希望使用某些 API 与服务器进行交互。您需要将每个方法和响应定义为 Enum。然后你需要添加 toString 和 valueOf 方法。为什么不使用字符串?
回答by Johannes Schaub - litb
I've learned the "method of least surprise". Instinctively, using enum is the right thing. So i would go for it. I'm sure the Java makers think alike.
我已经学会了“最少惊喜的方法”。本能地,使用枚举是正确的。所以我会去的。我相信 Java 制造商的想法是一样的。
Edit: Excellent explanation of POLS: http://benpryor.com/blog/2006/06/29/api-design-the-principle-of-least-surprise/
编辑:POLS 的精彩解释:http://benpryor.com/blog/2006/06/29/api-design-the-principle-of-least-surprise/
回答by Martin v. L?wis
Usage of strings in existing APIs is not bad practice; it is bad practice to change the APIs just because Java has now support for enums. For new APIs, I agree with what everybody else said.
在现有 API 中使用字符串是不错的做法;仅仅因为 Java 现在支持枚举就更改 API 是不好的做法。对于新的 API,我同意其他人所说的。

