枚举vs int标志? [英] Enumerations vs int flags?

查看:103
本文介绍了枚举vs int标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想就此事提出一些意见。我一直使用int标志,如果我在Java中使用枚举,只是对可能的易用性表现感到好奇吗?

Just wanted some opinions on the matter. I have always used int flags, and was just curious on possible performance of ease of use if I were to use enumerations in Java?

推荐答案

<我非常怀疑你会看到性能的好处 - 在某些情况下,甚至可能会有性能损失,具体取决于你如何使用它们。我怀疑它们是否具有重要意义。

I very much doubt you'd see performance benefits - and in some cases there may even be performance penalties, depending on how you're using them. I doubt they'd be significant though.

好处不在于表现 - 它是用语言表达或更清楚地表达你的意图,导致更具可读性(和类型安全的代码。例如,对于整数标志,如果您尝试在方法调用中使用(比方说)HTTP_STATUS_UNAUTHORIZED作为文件共享模式的值,则不会抱怨...如果两者都是枚举,则参数将被强类型化为真的只允许文件共享模式(或者,假设你 谈论Java),这是空的。

The benefit isn't in terms of performance - it's in terms or expressing your intentions more clearly, leading to more readable (and type-safe) code. For example, with integer flags nothing's going to complain if you try to use (say) HTTP_STATUS_UNAUTHORIZED as a value for your file sharing mode in a method call... whereas if both of those were enums, the parameter would be strongly typed to really only allow file sharing modes (or null, admittedly, assuming you are talking about Java).

这篇关于枚举vs int标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆