用Indef和StringDef代替枚举

最近一直在了解一些性能优化上的知识,有一条是这样的:

不要过多的使用枚举,枚举占用的的内存空间比整型大。

那不使用枚举,那怎么搞呢?首先,我们要清楚,枚举有什么用。知道枚举的用处以后我们才能根据它的用处来代替它。

看一下这里:http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html,有这么一段话:

 

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

Because they are constants, the names of an enum type's fields are in uppercase letters.

枚举类型是一种特殊的数据类型,一组已经定义好了的常量,变量必须等于已经预定义的常量之一。……枚举必须是大写……

还有这段:

Java programming language enum types are much more powerful than their counterparts in other languages. The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared. This method is commonly used in combination with the for-each construct to iterate over the values of an enum type. For example, this code from the Planet class example below iterates over all the planets in the solar system.

Java中枚举类型比其他的同行在其他语言中更强大。当创建了一个枚举的时候,编译器会自动添加一些特殊的方法,例如,他们有一个静态值方法,该方法返回一个包含所有声明它们顺序枚举值的数组。

相对于int静态常量来说,枚举最大的优势还是提供了类型安全。

那怎么办呢?查了资料,发现谷歌官方也注意到了这点,官方推出了两个注释:IntDef和StringDef,用来提供编译器到类型检查来替代枚举的作用。

来看一下Android Developers给的官方解释:

Denotes that the annotated element of integer type, represents a logical type and that its value should be one of the explicitly named constants. If the IntDef#flag() attribute is set to true, multiple constants can be combined.

表示该整数类型的注释元素,代表着一种逻辑类型,它的价值应该是明确的命名常量之一。如果IntDef标志的属性设置为true,多种常量可以结合起来。

根据官方给的解释,是不是给人一种跟枚举的功能基本差不多的感觉。ok ,下面我们来看一下怎么去使用这个注解:

还是直接上代码:

 

public class MainActivity extends Activity {
public static final int MALE = 0;
public static final int FEMALE = 1;
@Bind(R.id.button)
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    Person person = new Person();
    person.setSex(MALE);
    button.setText(person.getSexDes());
}
class Person {
    @SEX
    private int sex;
    public void setSex(@SEX int sex) {
        this.sex = sex;
    }
    @SEX
    public int getSex() {
        return sex;
    }
    public String getSexDes() {
        if (sex == MALE) {
            return "男";
        } else {
            return "女";
        }
    }
}

@IntDef({MALE, FEMALE})
@Retention(RetentionPolicy.SOURCE)
public @interface SEX {
}
}

 

 

很麻烦的一点是:当我们直接赋值一个int 给setSex()的话,编译器会报错,但是代码是没有问题的,仍然可以正确运行,这就牵扯到一个Android Studio的静态代码检查问题了,不知道的同学可以去了解一下。

 

 

很简单,很实用,对吧!
 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值