依据:
Enums often require more than twice as much memory as static constants. You should strictly avoid using enums on Android
枚举比静态常量用更多的内存,慎用
解决方案:
官方推出了两个注解,IntDef和StringDef,用来提供编译期的类型检查,如果输入类型错误,编译器就会提示报错,但仍可以运行。因为代码本身是RetentionPolicy.SOURCE级别的,只能给代码检查工具使用。
demo:
定义intDef
public class LoadType {
public static final int TYPE_REFRESH_SUCCESS = 1;
public static final int TYPE_REFRESH_ERROR = 2;
public static final int TYPE_LOAD_MORE_SUCCESS = 3;
public static final int TYPE_LOAD_MORE_ERROR = 4;
@IntDef({TYPE_REFRESH_SUCCESS, TYPE_REFRESH_ERROR, TYPE_LOAD_MORE_SUCCESS, TYPE_LOAD_MORE_ERROR})
@Retention(RetentionPolicy.SOURCE)
public @interface checker {
}
}
使用
void setKnowledgeSystemArticles(Article article, @LoadType.checker int loadType);
如果变换对应的数据类型或者值 就会提示报错,如: