Java:具有泛型类的方法类型参数的工厂 [英] Java: factory with method type parameters for class generic

查看:267
本文介绍了Java:具有泛型类的方法类型参数的工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

初始状态:



我为其他通用接口 A< T> 创建了一个工厂接口:

  public interface Factory< T,X extends A< T>> {
public X create();





问题描述:



现在我遇到了问题,我需要为每种类型的 T 实例化工厂。这变得非常不方便,特别是如果某些代码想从 A< T> 转换为 A< T2> ,其中 T2 是一些以前未知的类型,或者有很多不同的 T2 ,因此我不想为每个特例指定数百个工厂。 b
$ b

目标:



我想将类型 T 作为创建的通用参数方法。这样,我得到像(注意,不正确的Java :-)):

  public interface Factory< X extends A< ;? >> {
public< T> X< T>创建();


$


Factory的一个实现可能会简单地执行如下操作:

  public class Factory4B实现Factory< B> {
public< T> X< T> create(){
return new B< T>();


写下接口的上述版本会导致错误消息为create的返回值:


类型X不是通用的;它不能用参数参数化




问题:



实现这样一个通用工厂的方式,还是我需要使用完全不同的方法?我希望能够在类级别指定 X ,以便我可以轻松实例化 X 。我不想为每个参数创建一个工厂 T



感谢您提前回答此问题。

根据以下评论重新思考问题(27.4.15)



根据评论重新思考问题下面,我的问题是不可能实现的,因为无法保证泛型参数仍然存在于 A< T> 的子类中,换句话说:没有泛型参数的继承。



示例:


  • 让我们想一想一个从 A< String> 派生的类C。

  • 由于C没有类型参数,因此无法创建 C< Integer> 。因此,不存在可能创建 C< Integer>


  • 解决方案

    不,这不适用于泛型。由于类型擦除, T 类型在运行时不再存在,因此Java不知道要调用哪个构造函数。



    您必须找到一种方法在运行时将类型 T 传递给您的类。在Java中,您可以使用类 Class< S> 作为运行时类型令牌。然后你的代码可能看起来像这样:

      public< T> X< T> create(Class< T> classOfT){
    T t = classOfT.newInstance();
    返回新的X< T>(t);
    }


    Initial Situation:

    I created a factory interface for some other generic interface A<T>:

    public interface Factory<T, X extends A<T>> {
      public X create();
    }
    

    Problem Description:

    Now i have the problem that i need to instantiate a factory for every single type T. This becomes very unhandy, especially if some code wants to do some transformation from A<T> to A<T2>, where T2 is some previously unknown type or there are so many different T2, that i do not want to specify hundreds of factories for each special case.

    The Goal:

    I would like to pass the type T as a generic parameter of the create method. Such that i get something like (Attention, not correct Java :-) ):

    public interface Factory<X extends A<?>> {
      public <T> X<T> create();
    } 
    

    An implementation of Factory might then simply do something like:

    public class Factory4B implements Factory<B> {
      public <T> X<T> create() {
        return new B<T>();
      }
    } 
    

    Writing the above version of the interface down gives the error message for the return value of create:

    The type X is not generic; it cannot be parameterized with argument

    The Question:

    Is there some way to realize such a generic factory, or do i need to use a totally different approach? I want to be able to specify X at the class level, such that i can instantiate X easily. I do not want to create a single Factory for every parameter T

    Thank you for your time to answer this question in advance.

    Rethinking the problems based on the below comments (27.4.15)

    Rethinking the problem based on the comments below, my question is not possible to realize as it is not possible to guaranty that the generic parameters are still present in the subclasses of A<T>, or in other words: there is no inheritance of generic parameters.

    Example:

    • Lets think of a class C which is derived from A<String>.
    • It is not possible to create C<Integer> as C has no type argument.
    • Therefore, there exist no general Factory, that might create C<Integer>

    解决方案

    No, this is not possible with generics alone. Due to "type erasure" the type of T is not there anymore at runtime, so Java doesn't know what constructor to call.

    You'll have to find a way to pass the type of T to your class at runtime. In, Java you can use the class Class<S> as a runtime type token. Then your code might look something like this:

    public <T> X<T> create(Class<T> classOfT) {
        T t = classOfT.newInstance();
        return new X<T>(t);
    }
    

    这篇关于Java:具有泛型类的方法类型参数的工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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