简体   繁体   English

在类中实例化泛型类型:V扩展列表

[英]Instantiating Generic Type in class: V extends List

I'm trying to develop a class that works with objects who extend java.util.List, for example, ArrayList. 我正在尝试开发一个类,该类可与扩展java.util.List的对象一起使用,例如ArrayList。 Below is an example of what I think should compile (assuming the necessary import statements): 以下是我认为应编译的示例(假设必要的import语句):

public class MyClass<V extends List> {

    V myList = new ArrayList();

    MyClass<ArrayList> myinstance = new MyClass<>();
}

While the "myinstace" is no problem, the compiler fails when attempting to instantiate "myList.": 虽然“ myinstace”没有问题,但是尝试实例化“ myList”时,编译器将失败:

MyClass.java:[17,16] incompatible types: java.util.ArrayList cannot be converted to V MyClass.java:[17,16]不兼容的类型:java.util.ArrayList无法转换为V

As I understand generics, declaring: 据我了解,泛型声明:

<V extends List>

implies that whatever type "V" is, it must be a subclass of "List." 表示无论“ V”类型是什么,它都必须是“ List”的子类。 Thus, why cannot I instantiate as above, and is there any correct way to instantiate an object of type "V" within the class? 因此,为什么不能如上所述进行实例化,并且在类中是否有任何正确的方法来实例化“ V”类型的对象?

Furthermore, how might one return an object of type "V" within a method in the class? 此外,如何在类的方法中返回“ V”类型的对象?

public V getSomeData(){
    // How do we create our object of type "V"?
}

Background 背景

I have an interface, PatternElement: 我有一个接口PatternElement:

public interface PatternElement<D,W> {

    /**
     * The data that this element contains or signifies.
     * @return 
     */
    public D getData();

    /**
     * The width of this pattern element within a pattern.
     * @return 
     */
    public W getWidth();

}

I'm trying to develop a subclass, PolyPatternElement, which can contain many "data" and "width" items within itself. 我正在尝试开发一个子类PolyPatternElement,它本身可以包含许多“数据”和“宽度”项。 I thought the following code would accomplish this... 我以为下面的代码可以做到这一点...

public class PolyPatternElement<V extends List, W extends List> implements PatternElement<V,W>{


    @Override
    public V getData() {
        ArrayList data = new ArrayList();
        return data;
    }

    @Override
    public W getWidth() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

} 

Unfortunately, "getData" doesn't compile. 不幸的是,“ getData”无法编译。 Thus, I narrowed down the problem and asked the original question. 因此,我缩小了范围并提出了原始问题。 After the feedback, I think the better PolyPatternElement definition is: 反馈后,我认为更好的PolyPatternElement定义是:

public class PolyPatternElement<V,W> implements PatternElement<List,List>{


    @Override
    public List getData() {
        ArrayList data = new ArrayList();
        return data;
    }

    @Override
    public List getWidth() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

This allows declaring a PolyPatternElement object whose types are any implementation of List which was my intention. 这允许声明一个PolyPatternElement对象,其类型是List的任何实现(这正是我的意图)。

尝试(未试用):

public class PolyPatternElement<V, W> implements PatternElement<List<V>, List<W>>

... implies that whatever type "V" is, it must be a subclass of "List." ...表示无论“ V”类型是什么,它都必须是“ List”的子类。

Within a generic class you cannot simply instantiate an object of the generic parameters class. 在泛型类中,您不能简单地实例化泛型参数类的对象。

public class MyClass<V extends List> {

    V myList = new ArrayList();  // <-- What if 'V' is 'LinkedList'?

}

You either have to provide the class or an object with the constructor: 您必须提供带有构造函数的类或对象:

public class MyClass<V extends List> {

    private final V myList;

    public MyClass(V theList) {
        this.myList = theList;
    }

    public MyClass(Class<V> listType) {
        this.myList = listType.newInstance(); // works only if 'listType' has default constructor.
    }

}

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

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