Java:“enum”与“String”作为参数

发布于 2024-07-09 07:19:21 字数 226 浏览 7 评论 0原文

我一直在阅读 Systemsetget 方法的详细信息,但参数通常是字符串。

您是否认为自从包含 enum 以来使用 String 作为参数是不好的做法?

至少更好的替代方案可能是 public final String,不是吗?

I've been reading through the details of the System libraries set and get methods yet the parameters are usually Strings.

Would you consider the use of String as parameters bad practise since the inclusion of enum?

A better alternative at minimum might be public final String, No?

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

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

发布评论

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

评论(7

淡写薰衣草的香 2024-07-16 07:19:21

我认为枚举是比字符串更好的方法。 它们是类型安全的,并且比较它们比比较字符串更快。

作为 Java 1.5 之前的替代方案,您可以使用 Joshua Bloch 在他的《Effective Java》一书中建议的类型安全枚举模式。 对于类型安全枚举,另请参阅 http://www.javacamp.org/designPattern/enum.html< /a>

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

无人问我粥可暖 2024-07-16 07:19:21

如果您的参数集有限且在编译时已知,请使用enum

如果您的参数集在编译时是开放的且未知,请使用字符串。

If your set of parameters is limited and known at compile time, use enum.

If your set of parameters is open and unkown at compile time, use strings.

坚持沉默 2024-07-16 07:19:21

仅仅因为您将 public final String 声明为您希望作为参数传递到方法中的内容,就没有什么可以阻止我传递我喜欢的任何内容。

使用枚举意味着我无法创建自己的对象来传递,从而保护问题的双方。 我认为您应该使用常量字符串代替枚举的唯一一次是,如果您需要为用户留出空间来扩展您的方法以启用自定义功能......

Just because you declare a public final String as something you expect to have passed into a method as a parameter, there's nothing stopping me from passing in anything I like.

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...

无力看清 2024-07-16 07:19:21

如果您指的是 System.setProperty()、System.getProperty() 或 System.getenv(),我认为字符串在这里是合适的,因为可能的键集是开放的。 key 参数对应于某个文件或存储在某处的实际文本/字符串类型值。

如果您有一组封闭的键,我认为枚举会更受欢迎。

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.

If you have a closed set of keys, I think enums would be much preferred.

薄情伤 2024-07-16 07:19:21

尽管使用枚举是类型安全的,但将枚举转换为字符串(反之亦然)的需求非常频繁。 Java 中没有内置功能可以做到这一点。 最终您将使用 valueOf() 和 toString()。 使用这种方法与仅使用字符串没有太大区别。 因为您需要处理字符串无法转换为枚举的情况。

因此,仅使用静态最终字符串很容易,并且是一种常见的做法,AFAIK。

例如,您需要使用某些 API 与服务器交互。 您需要将每个方法和响应定义为枚举。 然后您需要添加 toString 和 valueOf 方法。 为什么不使用字符串呢?

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.

So just using static final strings is easy and is a common practice, 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?

缪败 2024-07-16 07:19:21

我学会了“最不意外的方法”。 直觉上,使用枚举是正确的。 所以我会去做。 我确信 Java 开发者也有同样的想法。

编辑:POLS的精彩解释:http://benpryor.com/blog/2006/06/29/api-design-the-principle-of-least-surprise /

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.

Edit: Excellent explanation of POLS: http://benpryor.com/blog/2006/06/29/api-design-the-principle-of-least-surprise/

梦幻的心爱 2024-07-16 07:19:21

在现有 API 中使用字符串并不是一个坏习惯; 仅仅因为 Java 现在支持枚举就更改 API 是不好的做法。 对于新的 API,我同意其他人的说法。

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.

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