註解代替枚舉

都知道枚舉在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){
//方法體
}
發佈了96 篇原創文章 · 獲贊 74 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章