在android中将参数化的枚举转换为枚举的注释 [英] Convert parametized Enum to Enumerated Annotation in android

查看:21
本文介绍了在android中将参数化的枚举转换为枚举的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 andriod @IntDef 注释的问题.我知道在其基本用法中,它应该替换 enum.但是如果例如,我有一个带有多个硬连线值的参数化枚举

I have a question regarding to the andriod @IntDef Annotation. I know that in its basic usage, it should replace the enum. But what if I have a parameterized enum with multiple hardwired values for example

public enum MyEnum {
  YES(true, 1),
  NO(false, 0);


  private boolean boolState;
  private boolean intState;

  MyEnum(boolean boolState, int intState) {
    this.boolState = boolState;
    this.intState = intState;
  }

  public boolean getBoolState() {
    return boolState;
  }

  public int getIntState() {
    return intState;
  }
}

这将如何被 Android 中的枚举注释取代?

在这种情况下,这样做是否具有暗示性?我搜索过到处都是,但我还没有找到任何答案.

Is it even suggestive to do something like that in this case? I searched everywhere, but I haven't found any answer for that.

先谢谢你!

推荐答案

我不认为你能找到任何东西,因为:

I don't think you would be able to find anything because:

IntDef 是一种替换有参数的整数枚举的方法应该只接受显式的 int 值.

IntDef is a way of replacing an integer enum where there's a parameter that should only accept explicit int values.

您可以在此处阅读更多相关信息.枚举注解用于简单类型,你也可以将它用于字符串StringDef.当您需要其功能时使用枚举.不要严格避免它.对于您的情况,我认为创建类而不是枚举看起来像这样:

you can read more about it here. Enumerated annotations are for simple types, you could use it for strings also StringDef. Use enum when you need its features. Don't avoid it strictly. For your case I think creating class instead of enum would look like this:

public class MyEnum {

    public static final MyEnum YES = new MyEnum(true, 1);
    public static final MyEnum NO = new MyEnum(false, 0);

    private boolean boolState;
    private int intState;

    MyEnum(boolean boolState, int intState) {
        this.boolState = boolState;
        this.intState = intState;
    }

    public boolean getBoolState() {
        return boolState;
    }

    public int getIntState() {
        return intState;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        MyEnum myEnum = (MyEnum) o;

        return boolState == myEnum.boolState && intState == myEnum.intState;
    }

}

并且您可以在代码中使用常量.但是如果使用枚举,您将进行类型检查(您将只能接受列出的值)和方法重载(每个枚举常量都可以有自己的方法实现).如果您想使用更少的空间并且这是您想避免使用 enum 的唯一原因,我建议您这样做不值得.

and you could use constants in your code. But if using enums you will have type checking (you'll be able to accept only listed values) and method overloading (every enum constant can have its own implementation of a method). If you want to use less space and that is the only reason why you want to avoid using enum I would suggest you that it's not worth it.

这篇关于在android中将参数化的枚举转换为枚举的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆