简体   繁体   English

尝试使用泛型对类进行子类型化

[英]Trying to sub-type classes using generics

I am having problems trying to create a sub-type class, and calling its superconstructor with an ArrayList of objects of a certain generic type. 我在尝试创建子类型类并用某个通用类型的对象的ArrayList调用其超构造函数时遇到问题。 I am trying to get this code to work. 我正在尝试使此代码正常工作。 I want my SubTypes to ONLY accept a list of GenericObjects that are of type X . 我希望我的子类型仅接受类型XGenericObjects列表。

    public class SubType extends SuperType{

        public SubType(ArrayList<GenericObject<Y>> g) {
            super(g);
        }
    }

    public class SuperType {

        public ArrayList<GenericObject<?>> data = new ArrayList<GenericObject<? extends X>>();

        public SuperType(ArrayList<GenericObject<? extends X>> series) {

            data.addAll(series);
        }
    }


    class GenericObject<T extends X>{}
    class X{};
    class Y extends X{};

I get an error where the super(g) is called. 在调用super(g)时出现错误。 It says that 它说

The constructor TestGenerics.SuperType(ArrayList>) is undefined 构造函数TestGenerics.SuperType(ArrayList>)未定义

Thats kind of strange because I thought that Generics allowed this kind of thing. 那有点奇怪,因为我认为泛型允许这种事情。

If I don't pass through an ArrayList of my generic object, and just pass the generic object through then it is OK. 如果我不传递通用对象的ArrayList,而只是传递通用对象,那就可以了。

public class SubType extends SuperType{

    public SubType(GenericObject<Y> g) {
        super(g);
    }
}

public class SuperType {

    public ArrayList<GenericObject<?>> data = new ArrayList<GenericObject<? extends X>>();

    public SuperType(GenericObject<? extends X> series) {

        data.add(series);
    }
}

class GenericObject<T extends X>{
}

class X{};
class Y extends X{};

The above works perfectly. 以上完美地工作。 Is there any way to get the first example to work? 有什么方法可以使第一个示例生效?

Your problem is that ArrayList<Child> does not extend ArrayList<Parent> even if Child extends Parent . 您的问题是,即使Child扩展ParentArrayList<Child>也不会扩展ArrayList<Parent> However, you were on the right track before, because ArrayList<Child> does extend ArrayList<? extends Parent> 但是,您之前走在正确的轨道上,因为ArrayList<Child>确实扩展了ArrayList<? extends Parent> ArrayList<? extends Parent> . ArrayList<? extends Parent>

You just need to repeat your ? extends ... 您只需要重复您的? extends ... ? extends ... pattern one more time , like this: ? extends ...模式 ,如下所示:

public SuperType(ArrayList<? extends GenericObject<? extends X>> series) {
    data.addAll(series);
}

This compiles fine for me. 这对我来说很好。


UPDATE : The full code would look like this: 更新 :完整的代码如下所示:

public class SubType extends SuperType{

    public SubType(ArrayList<GenericObject<Y>> g) {
        super(g);
    }
}

public class SuperType {

    public ArrayList<GenericObject<?>> data = new ArrayList<GenericObject<? extends X>>();

    public SuperType(ArrayList<? extends GenericObject<? extends X>> series) {

        data.addAll(series);
    }
}


class GenericObject<T extends X>{}
class X{};
class Y extends X{};

Are you perhaps in search of this solution? 您是否正在寻找这种解决方案?

class SubType extends SuperType<Y> {

    public SubType(List<GenericObject<Y>> g) {
        super(g);
    }
}

class SuperType<T extends X> {

    public List<GenericObject<T>> data = new ArrayList<GenericObject<T>>();

    public SuperType(List<GenericObject<T>> series) {
        data.addAll(series);
    }
}

class GenericObject<T extends X>{}
class X{};
class Y extends X{};

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

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