枚举作为泛型类型并用于某些注释

问题描述 投票:-2回答:1

我希望子类将特定的枚举值传递给extends中的父类,这样这些特定的枚举值将在某些注释中的父类中使用。以下是我想要实现的目标。

public enum SomeEnum {
    VAL1, VAL2;
}

public @interface SomeAnnotation {
    SomeEnum someValue();
}

//below starts all fuzzy things : I want a specific value of enum 
public class parent<FetchType> {// not sure what exactly should write between  < >

    // below,at someValue I want the specific enum passed by child , means
    // the value passed between < and > 
    @SomeAnnotation(someValue = null /* here  should be the specific enum value passed 
    as type parameter of this class */) 
    private String someString;
        //getter-setter
}
//as i said , for child1 , i need SomeEnum.VAL1 to be passed to parent and
// SomeEnum.VAL2 for child2.
public class child1 extends parent<SomeEnum.VAL1>{}
public class child2 extends parent<SomeEnum.VAL2>{}
java generics enums annotations type-parameter
1个回答
0
投票

我会走出去,并建议你不需要Enumannotations,但也许类似下面的内容?

interface NotEnum  {
  interface Val1 extends NotEnum {}
  interface Val2 extends NotEnum {}
}

class Parent<T extends NotEnum> {...}

class Child1 extends Parent<NotEnum.Val1> {...}
class Child2 extends Parent<NotEnum.Val2> {...}
© www.soinside.com 2019 - 2024. All rights reserved.