android 如何优雅的使用枚举(@IntDef、@Strin

2022-12-01  本文已影响0人  Kael_Zhang的安卓笔记

引言

无论哪种开发语言,枚举的使用都是基础中的基础,不过在实际使用中,比如在 android 开发中,并不推荐直接使用枚举,为什么呢?占用内存多!

Android 如何优雅的使用枚举

@IntDef、@StringDef的用法

    //定义一组数据
    @IntDef({TYPE_0 ,TYPE_1 ,TYPE_2})
    @Retention(RetentionPolicy.SOURCE)
    private  @interface ENUM_TYPE{}

    public static final int TYPE_0 = 0;
    public static final int TYPE_1 = 1;
    public static final int TYPE_2 = 2;

    //成员注解,赋值时约束范围
    public @ENUM_TYPE int type;
    //方法注解,传参时约束范围
    public void setType(@ENUM_TYPE int type) {
        this.type = type;
    }

kotlin 密封类的用法

kotlin 密封类的用法

上一篇下一篇

猜你喜欢

热点阅读