1. 程式人生 > >註解代替列舉

註解代替列舉

都知道列舉在Android java 中使用會出現一些問題,使用多了還可能出現ANR異常,
但是很多時候不得不用,

在使用融雲的時候自定義訊息就是使用的這種方法

Java 中有@StringDef 和@intDef

public class BaseConst {
    public static final String TYPE_1 = "1";
    public static final String TYPE_2 = "2";
    @StringDef({TYPE_1, TYPE_2})
    public @interface Type {
    }
}


----------
public
class BaseConst { public static final String TYPE_1 = 1; public static final String TYPE_2 = 2; @IntDef({TYPE_1, TYPE_2}) public @interface Type { } }

在使用的時候

//作為常量使用
@BaseConst.Type
public String type 

//作為方法使用
public void func(@BaseConst.Type String type){
//方法體
}