简体   繁体   English

在它的抽象超类中使用泛型类型的子类?

[英]Using a generic type of a subclass within it's abstract superclass?

Within my code a have the following abstract superclass 在我的代码中,a有以下抽象超类

public abstract class AbstractClass<Type extends A> {...}

and some child classes like 和一些儿童班一样

public class ChildClassA extends AbstractClass<GenericTypeA> {...}

public class ChildClassB extends AbstractClass<GenericTypeB> {...}

I'm searching for an elegant way how I can use the generic type of the child classes (GenericTypeA, GenericTypeB, ...) inside the abstract class in a generic way. 我正在寻找一种优雅的方式,我可以通用的方式在抽象类中使用子类的泛型类型(GenericTypeA,GenericTypeB,...)。

To solve this problem I currently defined the method 为了解决这个问题,我目前定义了这个方法

protected abstract Class<Type> getGenericTypeClass();

in my abstract class and implemented the method 在我的抽象类中实现了该方法

@Override
protected Class<GenericType> getGenericTypeClass() {
    return GenericType.class;
}

in every child class. 在每个儿童班。

Is it possible to get the generic type of the child classes in my abstract class without implementing this helper method? 是否可以在我的抽象类中获取子类的泛型类型而不实现此帮助器方法?

BR, BR,

Markus 马库斯

I think its possible. 我认为这是可能的。 I saw this was being used in the DAO patterns along with generics. 我看到这被用在DAO模式和泛型中。 eg Consider classes: 例如考虑课程:

public class A {}
public class B extends A {}

And your generic class: 而你的通用类:

  import java.lang.reflect.ParameterizedType;
  public abstract class Test<T extends A> {

     private Class<T> theType;

     public Test()  {
        theType = (Class<T>) (
               (ParameterizedType) getClass().getGenericSuperclass())
              .getActualTypeArguments()[0];
     }

     // this method will always return the type that extends class "A"
     public Class<T> getTheType()   {
        return theType;
     }

     public void printType() {
        Class<T> clazz = getTheType();
        System.out.println(clazz);
     }
  }

You can have a class Test1 that extends Test with class B (it extends A) 你可以有一个类Test1,用类B扩展Test(它扩展了A)

  public class Test1 extends Test<B>  {

     public static void main(String[] args) {
        Test1 t = new Test1();

        Class<B> clazz = t.getTheType();

        System.out.println(clazz); // will print 'class B'
        System.out.println(printType()); // will print 'class B'
     }
  }

I'm not sure I fully understand your question - <Type> is the generic type of the subclass, even when it's being expressed in the abstract class. 我不确定我是否完全理解你的问题 - <Type> 子类的泛型类型,即使它是在抽象类中表达的。 For example, if your abstract superclass defines a method: 例如,如果您的抽象超类定义了一个方法:

public void augment(Type entity) {
   ...
}

and you instantiate a ChildClassA , you'll only be able to call augment with an instance of GenericTypeA . 并且您实例化一个ChildClassA ,您只能使用GenericTypeA实例调用augment

Now if you want a class literal, then you'll need to provide the method as you indicated. 现在,如果你想要一个类文字,那么你需要按照你的指示提供方法。 But if you just want the generic parameter, you don't need to do anything special. 但是,如果您只想要通用参数,则无需执行任何特殊操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM