Android为什么不使用更多的枚举? [英] Why doesn't Android use more enums?

查看:108
本文介绍了Android为什么不使用更多的枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





  • 它们导致更易读的代码。

  • 将枚举设置为

  • 它们可以轻松发现变量或参数的允许值。

  • 我读过的一切表示它们在C#和大多数JVM中的执行方式一样好。



但是,Android框架有很多种情况,类型需要传递,但是似乎没有使用枚举。一些例子,我认为他们的使用将是有益的是 Toast.LENGTH_SHORT / Toast.LENGTH_LONG View.GONE View.VISIBLE 等。



为什么这是?在Dalvik中,枚举是否比简单的整数值更差?有没有其他的缺点我不知道?

解决方案

这个答案是截至2011年3月的过期



枚举可以在Froyo上使用,根据这个答案(为什么从Android VM小组的成员(以及)中的避免枚举,您只需要Ints /2010/09/java-benchmarks.htmlrel =nofollow noreferrer>他的博客)。






< h2>以前的答案:

Android团队的官方建议是避免使用枚举:


枚举非常方便,但
不幸的是,当大小
和速度问题时,可能会很痛苦。例如:

  public enum Shrubbery {GROUND,CRAWLING,HANGING} 


将$ d

添加到
您的.dex文件相比,
等价类与三个public
static final ints相比。在首次使用时,
类初始化程序调用
方法,代表枚举值中
的每个对象。每个对象
获取自己的静态字段,
全集存储在一个数组中(
静态字段叫做$ VALUES)。这是
很多代码和数据,只为三个
整数。另外,这个:

 灌木灌木=灌木丛; 

导致静态字段查找。如果
GROUND是一个静态final int,
编译器会把它当作一个已知的
常量并内联。


资料来源:避免枚举您只需要Ints


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?

解决方案

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

这篇关于Android为什么不使用更多的枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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