【问题标题】:Enum as a generic type and to be used in some annotations枚举作为泛型类型并在某些注释中使用
【发布时间】:2013-02-08 17:55:11
【问题描述】:

我希望子类将特定的枚举值传递给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>{}

【问题讨论】:

  • 在您的问题中包含一个问题很有帮助。
  • @JamesMontagne :问题已经在代码中以 cmets 的形式出现。虽然我现在正在添加以更好地理解。
  • 我无法理解这个问题。 “我想要孩子传递的特定枚举”是什么意思。我不确定注释是否是解决您要解决的任何问题的正确方法
  • 枚举值不能作为泛型类的类型参数
  • 枚举是值,而不是类型。它们不能用于类型参数。

标签: java generics enums annotations type-parameter


【解决方案1】:

我会冒昧地建议您不需要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> {...}

【讨论】:

    猜你喜欢
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    相关资源
    最近更新 更多