我如何在一个列表中拥有Base和Sub-Classes的实例? [英] How can I have instances of Base and Sub-Classes in one List?

查看:181
本文介绍了我如何在一个列表中拥有Base和Sub-Classes的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



虽然只有一个对象作为参数(单个方法),但编译器不会抱怨。

p>

但是,如果涉及到列表,编译器会迫使我将列表声明为<?扩展Base>



之后,我不再允许将基本类型的对象添加到该列表中。



如何在一个列表中使用两种类型(Base和Subclass)?

  public class泛型{

class Base {}

class Sub extends Base {}
$ b $ interface I {
public void list(List< Sub> ; list);
public void single(Sub p);
}

class C implements I {
public void list(List< Sub> list){}
public void single(Sub p){}
}

void test(){
C c = new C();
c.single(new Sub());
c.list(new ArrayList< Base>()); // Generics.C类型的方法列表(List< Generics.Sub>)不适用于参数(ArrayList< Generics.Base>)

}

public static void main(String [] args){
Generics g = new Generics();
g.test();



$ div $解析方案

更改:

  public void list(List< Sub> list); 

到:

  public void list(List< ;? extends Base> list); 

只要使用 List< Base> 你编译这样的错误:

  public static void main(String [] args){
List< Sub> subs = new ArrayList< Sub>();
doSomethingWith(subs); // Main类型的doSomethingWith(List< Base>)方法不适用于参数(List< Sub>)
}

private static void doSomethingWith(List< Base> bases ){
//用碱基做
}

重新传递是列表< Base> doSomethingWith ,那么这一点是没有意义的,因为这不会给你一个编译器错误。如果您想传递特定类型的列表(例如上面的 List< Sub> ),那么您需要更改 doSomethingWith
pre $ private static void doSomethingWith(List< ;? extends Base> bases){
code>

解决了这个问题。您也可以在来电者手柄上进行操作(但它有点麻烦):

 列表< Sub> subs = new ArrayList< Sub>(); 
doSomethingWith(new ArrayList< Base>(subs));

通配符有一个问题()方法是你不能将新项目添加到列表中。要做到这一点,你需要这样的东西:

  private static< B extends Base> void doSomethingWith(List< B> base){

然后只添加 B 个例 c


I'm currently facing an issue with base and subclasses.

While having a single object as parameter (method single) the compiler doesn't complain.

But if it comes to lists the compiler forces me to declare the list as <? extends Base>

After that I'm no longer allowed to add objects of the base type to that list.

How can I use both types (Base and Subclass) in one list?

public class Generics {

    class Base {    }

    class Sub extends Base{     }

    interface I {
        public void list( List<Sub> list );
        public void single( Sub p);
    }

    class C implements I {
        public void list( List<Sub> list) {     }
        public void single( Sub p) {        }
    }

    void test() {
        C c = new C();
        c.single( new Sub() );
        c.list( new ArrayList<Base>() ); // The method list(List<Generics.Sub>) in the type Generics.C is not applicable for the arguments (ArrayList<Generics.Base>)

    }

    public static void main( String[] args) {
        Generics g = new Generics();
        g.test();
    }
}

解决方案

Change:

public void list(List<Sub> list);

to:

public void list(List<? extends Base> list);

Using just List<Base> will give you compiler errors like this one:

public static void main(String[] args) {
    List<Sub> subs = new ArrayList<Sub>();
    doSomethingWith(subs); // The method doSomethingWith(List<Base>) in the type Main is not applicable for the arguments (List<Sub>)
}

private static void doSomethingWith(List<Base> bases) {
    // Do something with bases
}

If all you're going to pass is List<Base> to doSomethingWith, then this point is moot, since this won't give you a compiler error. If you want to pass lists that are of a specific type (such as List<Sub> above), then you need to change doSomethingWith to:

private static void doSomethingWith(List<? extends Base> bases) {

This fixes the problem. You could also do it at the caller lever (but it's a bit messier):

    List<Sub> subs = new ArrayList<Sub>();
    doSomethingWith(new ArrayList<Base>(subs));

One issue with the wildcard (?) approach is that you can't add new items to the list. To do that, you need something like:

private static <B extends Base> void doSomethingWith(List<B> bases) {

And then add only B instances to bases.

这篇关于我如何在一个列表中拥有Base和Sub-Classes的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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