简体   繁体   English

Android:enum vs static final int?

[英]Android: enum vs static final ints?

What are the advantages (or disadvantages) of having an enum versus having a set of static final int s in Java Android applications? 在Java Android应用程序中使用enum与拥有一组static final int的优点(或缺点)是什么? Are there efficiency or performance optimizations that occur that favor the use of one or the other? 是否存在有利于使用其中一种或另一种的效率或性能优化?

I ask this in context of say intent requestCodes and such - which tend to be ints in the Android sample code, as opposed to values from an enum, which I was used to in C. 我在上下文中询问了这个请求代码等等 - 它往往是Android示例代码中的内容,而不是我在C中习惯的枚举值。

Enum advantages from this question : Enum从这个问题中获益:

  • 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. 将枚举设置为无效值比使用int或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. 我读过的所有内容都表明它们在C#和大多数JVM中的表现与整数一样好。

I would add: 我想补充一下:

  • Enums can have member and instance variables, whereas an int can't. 枚举可以包含成员和实例变量,而int则不能。

Like most abstractions, they are generally unequivocally advantageous once their performance catches up . 像大多数抽象一样,一旦他们的表现赶上 ,它们通常是非常有利的。 Especially in your application code (as opposed to framework code) I would choose enums over other methods that simulate them. 特别是在您的应用程序代码中(而不是框架代码),我会选择枚举而不是其他模拟它们的方法。

A very simple answer from personal experiences would be that Enums provide much better type safety or in other words the compiler gets to play a more active role in keeping your code bug free. 从个人经验中得出的一个非常简单的答案是,Enums提供了更好的类型安全性,换句话说,编译器可以在保持代码bug免费方面发挥更积极的作用。

On the other hand, because Enums are "second-class citizens" of the object world, they can be difficult to use in some of the more subtle design patterns commonly used today, especially when generics are involved. 另一方面,因为Enums是对象世界的“二等公民”,所以它们很难用于当今常用的一些更微妙的设计模式,特别是涉及泛型时。

And finally, you can use static final ints in a bitfield. 最后,您可以在位域中使用静态最终整数。 you couldnt do the following with an enum: 你不能用枚举做以下事情:

int selectedOptions = Options.OPTION1 | Options.OPTION2 | Options.OPTION3;

Well... according to a bald guy Enums are really bad for memory . 嗯......根据一个秃头男人的说法,Enums 真的很难记忆

You should use @IntDef/@StringDef annotations: 你应该使用@ IntDef / @ StringDef注释:

public static final int NAVIGATION_MODE_STANDARD = 0;
public static final int NAVIGATION_MODE_LIST = 1;
public static final int NAVIGATION_MODE_TABS = 2; 

@IntDef({NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS})
public @interface NavigationMode {}

and then 然后

@NavigationMode
public abstract int getNavigationMode();

public abstract void setNavigationMode(@NavigationMode int mode);

One advantage of ints over enums is in a CLASS FACTORY. 对枚举进行整数的一个优点是在CLASS FACTORY中。 The following C# code is not extensible: 以下C#代码不可扩展:

class Factory
{
    public enum DrawableType {CIRCLE,SQUARE};
    public static Drawable GetInstance(DrawableEnum e)
    {
        if (e == DrawableType.CIRCLE)
        {
            return new Circle();
        }
        else if (e == DrawableType.SQUARE)
        {
            return new Square();
        }
        else
        {
            throw new IndexOutOfRangeException(); // should never get here
        }
    }

I wrote this poor code. 我写了这个糟糕的代码。 Reviewing Design Patterns, the gang of four used an int. 回顾设计模式,四人一组使用了int。 I tried to recover here . 我试图在这里恢复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM