为什么 Android 不使用更多的枚举?

发布于 2024-10-15 05:16:43 字数 456 浏览 2 评论 0原文

我开始非常喜欢在代码中使用 C# 和 Java 枚举,原因如下:

  • 它们比整数、字符串或布尔标志集更加类型安全。
  • 它们会产生更具可读性的代码。
  • 将枚举设置为无效值比 int 或字符串更困难。
  • 它们可以轻松发现变量或参数的允许值。
  • 我读到的所有内容都表明它们在 C# 和大多数 JVM 中的性能与整数一样好。

然而,Android框架有很多需要传递各种类型标志的情况,但它们似乎都没有使用枚举。我认为使用它们会有所帮助的几个示例是 Toast.LENGTH_SHORT / Toast.LENGTH_LONGView.GONEView.VISIBLE

这是为什么呢? Dalvik 中枚举的性能是否比简单整数值差?还有其他我不知道的缺点吗?

I've started to really like using C# and Java enums in my code for several reasons:

  • They are much more type-safe than integers, strings, or sets of boolean flags.
  • They lead to more readable code.
  • It's more difficult to set an enum to an invalid value than an int or string.
  • They make it easy to discover the allowed values for a variable or parameter.
  • Everything I've read indicates that they perform just as well as integers in C# and most JVMs.

However, the Android framework has numerous cases where flags of various types need to be passed around, but none of them seem to use enums. A couple of examples where I would think their use would be beneficial are Toast.LENGTH_SHORT / Toast.LENGTH_LONG and View.GONE, View.VISIBLE, etc.

Why is this? Do enums perform worse than simple integer values in Dalvik? Is there some other drawback I'm not aware of?

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

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

发布评论

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

评论(3

伏妖词 2024-10-22 05:16:43

此答案自 2011 年 3 月起已过时。

根据此答案,枚举可以在 Froyo 及更高版本上使用(为什么从 Android 性能提示中删除了“只需要整数时避免枚举”?)来自 Android VM 团队的成员(以及 他的博客)。


上一个答案:

官方 Android 团队建议尽可能避免枚举:

枚举非常方便,但是
不幸的是,当尺寸变大时可能会很痛苦
和速度很重要。例如,这个:

公共枚举灌木丛 { GROUND, CRAWLING, HANGING }

添加 740 字节
您的 .dex 文件与
具有三个公共的等效类
静态最终整数。首次使用时,
类初始值设定项调用
代表每个对象的方法
枚举值。每个对象
获得自己的静态字段,并且
全套存储在一个数组中(a
称为“$VALUES”的静态字段)。那是
大量代码和数据,仅三个
整数。另外,这个:

灌木丛 = Shrubbery.GROUND;

导致静态字段查找。如果
“GROUND”是静态最终整数,
编译器会将其视为已知的
常量并内联它。

来源:在只需要整数的地方避免使用枚举

This answer is out of date as of March 2011.

Enums can be used on Froyo and up - according to this answer (Why was “Avoid Enums Where You Only Need Ints” removed from Android's performance tips?) from a member of the Android VM team (and his blog).


Previous Answer:

The official Android team recommendation is to avoid enums whenever you can avoid it:

Enums are very convenient, but
unfortunately can be painful when size
and speed matter. For example, this:

public enum Shrubbery { GROUND, CRAWLING, HANGING }

adds 740 bytes to
your .dex file compared to the
equivalent class with three public
static final ints. On first use, the
class initializer invokes the
method on objects representing each of
the enumerated values. Each object
gets its own static field, and the
full set is stored in an array (a
static field called "$VALUES"). That's
a lot of code and data, just for three
integers. Additionally, this:

Shrubbery shrub = Shrubbery.GROUND;

causes a static field lookup. If
"GROUND" were a static final int, the
compiler would treat it as a known
constant and inline it.

Source: Avoid Enums Where You Only Need Ints

萧瑟寒风 2024-10-22 05:16:43

整数更小,需要的开销也更少,这在移动设备上仍然很重要。

Integers are smaller, and require less overhead, something that still matters on mobile devices.

无所谓啦 2024-10-22 05:16:43

我的一位同事针对这种情况做了一个小测试。他自动生成了一个
class 和一个具有相同数量“enum”的enum。我相信他生成了 30000 个条目。

结果是:

  • class.class 大约为 1200KB
  • enum.class 大约为 800KB

希望这个帮助某人。

A colleague of mine performed a small test regarding this situation. He auto generated a
class and an enum with the same amount of "enums". I believe he generated 30000 entries.

The results were:

  • .class for the class was roughly 1200KB
  • .class for the enum was roughly 800KB

Hope this helps someone.

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