本文介绍了何时使用Enum / Int常数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我们什么时候应该使用枚举,何时应该使用最终的常量?



我知道它已经在虽然是C#的问题。



我的问题是为什么Android使用这么多常量而不是枚举?例如,



在我看来,如果我们使用常量,可能会有以下风险:
如果我们定义一个LEVEL常数,

  public static final int LEVEL_LOW = 1; 
public static final int LEVEL_MEDIUM = 2;
public static final int LEVEL_HIGH = 3;当我们传递int = 4的参数时,

它不会有编译错误,如果我们传递一个数字1,代码读取器可能不容易知道它的意思。但是,枚举可以解决这个问题,尽管它可能会导致更多的开销,因为它是Object。



那么Android为什么使用常量而不是Enum?
有什么原则,在这种情况下我们什么时候应该使用常量或枚举?

解决方案

这与android历史。在Froyo之前的版本中有未经证实的性能问题。建议不要使用开发人员的枚举。由于Froyo Design Performance for Performance文档已按照所述进行了重写。

但是,改变结构没有意义的遗产内容。



性能可能与要求存储的String有关。每个常量与多个枚举之间的单个类的创建有显着的差异。



例如在Java 7中,当您有一个具有两个字段的枚举时,您需要在轮询常量中有44个项目,而对于具有两个静态最终整数的类,您只需要17. / p>

有什么区别

  class ContantField {
public static final int f1 = 0;
public static final int f2 = 1;
}

枚举ContantEnum {
E1,E2
}

这两个声明在存储和使用的方式上有很大的不同。简单的 ContantEnum 可能看起来像

  class ContantEnum {
public static final Enum enum0 = new Enum(V1,0);
public static final Enum enum1 = new Enum(V2,1);
public static final Enum [] values = new Enum [] {enum0,enum1};
}

通过这种简化,您可以注意到枚举需要比 int 更多的内存资源。



为了回答你的问题,必须了解枚举的作用枚举的一个作用是增加编译时类型的安全性。



要指出,请看这个例子:

  public void setImportantThing (int priviledge,int rights)

public void setImportantThing(Privilege p,Right r)

int 的情况下,我们可以传递任何int的值。在枚举中,我们被迫使用正确的。



我们在这里的情况是在运行时在编译时验证和内存使用之间进行权衡。您应该使用枚举,而 static int 是否足够安全。



注意:
枚举被引入了1.5版本的Java,在这之前使用它们是相当有问题的。



在Android Studio Beta版中,开发人员将能够使用注释来强制实施类型安全。


I have a question that when should we use Enum and when should we use a final constants?

I know that it has been discussed at Enums and Constants. Which to use when? though it is C# question.

My question is why Android use so many Constants rather than Enum? For example ,Context

In my opinion, if we use constants, there may be the risk that as below:if we define a LEVEL Constant that

 public static final int LEVEL_LOW=1;
 public static final int LEVEL_MEDIUM=2;
 public static final int LEVEL_HIGH=3;

when we pass a param of int =4. it will not have compile error, and if we pass a number of 1, the code reader may not easily know what it means.

But Enum can solve this problem though it may cause more overhead since it is Object.

So why Android uses constants instead of Enum?Is there any principle that when should we use constants or Enum in such case?

解决方案

This is related to android history. There were unconfirmed performance issues in versions before Froyo. It was recommended to not use enum by the developers. Since Froyo the Designing for Performance documentation was rewritten as described here.

But there was no point in changing the structure of legacy content.

The performance can be related to having String required to be stored. There is significant difference between the creation of a single class for every constants vs. multiple enums.

For example in Java 7 when you have a enum with two fields you need 44 items in poll constant and for a class with two static final integers you need only 17.

What is the difference

class ContantField {
  public static final int f1 = 0;
  public static final int f2 = 1;
}

enum ContantEnum {
  E1,E2
}

This two declarations are very different in the way there are stored and used. The simplification of ContantEnum could look like

class ContantEnum {
   public static final Enum enum0    = new Enum("V1",0);
   public static final Enum enum1    = new Enum("V2",1);
   public static final Enum[] values = new Enum[] {enum0,enum1};
}

By this simplification you can notice that enum require more memory resources than int.

To answer your question, it must be understood the role of enums. One role of enum is to increase compile time type safety.

To point that out see this example:

public void setImportantThing(int priviledge, int rights)

public void setImportantThing(Privilege p, Right r)

In the case of int we can pass any value that is an int. In he tcase of enum we are forced to use the proper one.

The case we have here is trade off between compile time validation and memory usage on runtime. You should decide for yourself when you should use enum and where static int is sufficiently secure.

Note: enum was introduced to Java in version 1.5, using them before this was quite problematic more.

In Android Studio Beta, the developer will be able to enforce type safety using annotation.

这篇关于何时使用Enum / Int常数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:47