首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Enum类中的Enum变量

Enum类中的Enum变量
EN

Stack Overflow用户
提问于 2020-05-22 10:39:14
回答 2查看 162关注 0票数 0

请为下列情况提供最佳方法:

有一张桌子,大约有20列。

每个列都有自己的短名称、全名和类型(数字或字符串)。

每种列类型都可以有自己的运算符-例如字符串-包含、等于;数字-多、少、==、!=。

每个操作员都可以有自己的描述。

我必须拥有表类的对象,并且能够查看它的所有列,查看每个列的短名称和全名,使用基于列类型的运算符。

我正在尝试使用枚举,但我不知道如何将特定列连接到特定类型。

例如,如何将"Id“列连接到"StringType”,将“服务”列连接到"NumberType“。你能帮帮我吗。

代码语言:javascript
复制
class Table{

    public enum Column {
        Id("id", "ID number"),
        Services("serv", "Services");

        private final String shortName;
        private final String fullName;

        Column(String shortName, String fullName) {
           this.shortName = shortName;
           this.fullName = fullName;
        }

        public String getShortName() {
            return shortName;
        }

        public String getFullName() {
            return fullName;
        }
    }


    public enum StringType{
        contains("String contain another string"),
        equals("String equal a string");

        private final String placeholder;

        StringType(String fullName) {
            this.placeholder = fullName;
        }

        public String getPlaceholder() {
            return placeholder;
        }
    }

    public enum NumberType{
        more("value that is more than input"),
        less("value that is less than input");

        private final String placeholder;

        NumberType(String fullName) {
            this.placeholder = fullName;
        }

        public String getPlaceholder() {
            return placeholder;
        }
    }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-05-22 15:22:18

与任何其他类一样,枚举类型可以实现接口。你可以利用这一点来发挥你的优势:

代码语言:javascript
复制
public interface DataType {
    // Deliberately empty.  This is a marker interface.
}

public enum StringType
implements DataType {
    // ...
}

public enum NumberType
implements DataType {
    // ...
}

public enum Column {
    Id("id", "ID number", StringType.class),
    Services("serv", "Services", NumberType.class);

    private final String shortName;
    private final String fullName;
    private final Class<? extends DataType> type;

    Column(String shortName, String fullName, Class<? extends DataType> type) {
       this.shortName = shortName;
       this.fullName = fullName;
       this.type = type;
    }

    // ...
}

如果计划实际使用这些方法来比较数据,可以将方法添加到DataType接口中:

代码语言:javascript
复制
public interface DataType<T> {
    Class<T> getDataClass();

    BiPredicate<? super T, ? super T> getTest();

    default boolean test(T value1, T value2) {
        return getTest().test(value1, value2);
    }

    default boolean testObjects(Object value1, Object value2) {
        Class<T> type = getDataClass();
        return test(type.cast(value1), type.cast(value2));
    }
}

public enum StringType
implements DataType<String> {
    contains("String contain another string", String::contains),
    equals("String equal a string", Object::equals);

    private final String placeholder;
    private final BiPredicate<? super String, ? super String> test;

    StringType(String fullName,
               BiPredicate<? super String, ? super String> test) {
        this.placeholder = fullName;
        this.test = test;
    }

    public String getPlaceholder() {
        return placeholder;
    }

    @Override
    public BiPredicate<? super String, ? super String> getTest() {
        return test;
    }

    @Override
    public Class<String> getDataClass() {
        return String.class;
    }
}

public enum NumberType
implements DataType<Number> {
    more("value that is more than input",
        (n1, n2) -> n1.doubleValue() > n2.doubleValue()),
    less("value that is less than input",
        (n1, n2) -> n1.doubleValue() < n2.doubleValue());

    private final String placeholder;
    private final BiPredicate<? super Number, ? super Number> test;

    NumberType(String fullName,
               BiPredicate<? super Number, ? super Number> test) {
        this.placeholder = fullName;
        this.test = test;
    }

    public String getPlaceholder() {
        return placeholder;
    }

    @Override
    public BiPredicate<? super Number, ? super Number> getTest() {
        return test;
    }

    @Override
    public Class<Number> getDataClass() {
        return Number.class;
    }
}

public enum Column {
    Id("id", "ID number", StringType.class),
    Services("serv", "Services", NumberType.class);

    private final String shortName;
    private final String fullName;
    private final Class<? extends DataType<?>> type;

    Column(String shortName, String fullName, Class<? extends DataType<?>> type) {
       this.shortName = shortName;
       this.fullName = fullName;
       this.type = type;
    }

    // ...
}
票数 2
EN

Stack Overflow用户

发布于 2020-05-22 10:51:55

Java有一个Class类,它可以将类型作为它的对象,甚至可以与原始类型一起工作。

它没有构造函数,而是一个名为forName()的工厂方法,它使用提供的字符串作为参数创建一个类(尽管这被认为是错误的做法)。

获得对类型的引用的更好方法是使用类文字。表示其基础类的Class对象也可以使用getClass()方法从其任何对象中获得。

下面是使用Class类创建表示类/类型的对象的一些方法:使用类文本创建

代码语言:javascript
复制
    Class<?> type1 = int.class;
    Class<?> type2 = boolean.class;

工厂方法

代码语言:javascript
复制
   Class<?> type1 = Class.forName("java.lang.String"); 

最好避免这种方法,因为涉及字符串解析,这可能导致不必要的运行时错误,正如@VGR所指出的。

使用对象

代码语言:javascript
复制
   String str = "";
   Class type<?> = str.getClass();

您可以添加一个类型为Class<?>的附加变量,并执行如下操作:

代码语言:javascript
复制
 public enum Column {
        Id("id", "ID number", String.class),
        Services("serv", "Services", Number.class);
        Contact("cont", "Contacts", long.class);

        private final String shortName;
        private final String fullName;
        private final Class<?> type;

        Column(String shortName, String fullName, Class<?> type) {
           this.shortName = shortName;
           this.fullName = fullName;
           this.type = type;
        }

        public String getShortName() {
            return shortName;
        }

        public String getFullName() {
            return fullName;
        }
       public Class<?> getType() {
            return type;
       }
    }

Class是一个强大的类,它有各种方法,如getName()getMethods()。更多关于它的这里

注意: Class通常用于反射,这有主要的缺点,它基本上可以破坏封装,还需要一些性能开销。但是,如果您只是使用这个额外的字段来存储类型信息,那就不是什么大事了。

**这是假设您要将列类型映射到Java类型!**

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61953120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档