如何将继承对象的列表转换为Java中的对象集合? [英] How to cast a list of inheriting objects to a collection of objects in Java?

查看:226
本文介绍了如何将继承对象的列表转换为Java中的对象集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集合类型:

 集合< A> collecA 

我在我的对象中有一个列表:

 列表< B> listB 

B扩展A

  class B extends A {...} 

请执行以下操作:

  collecA = listB 

我不明白为什么自从Collection实现List之后。

解决方案

让我们假设你可以做你所描述的:

  class B extends A {...} 

Collection< A> collecA;
List< B> listB;

collecA = listB; //通常一个错误,但允许假装它允许

collecA.add(new A()); //问题!

方法调用 collecA.add(new A())显示okay,因为 collecA 是一个集合
,它拥有 A 但是,如果允许上面的赋值,那么我们有一个
的问题,因为 collecA 真的引用 List< B& code> instance - 我只是
添加 A 到只能保存 B s!



Asker也说:


集合由List实现。


Collection是List的超类并不重要。

  class B extends A {...} 
List< A> listA;
List< B> listB;
listA = listB; //仍然有错误,仍然导致相同的问题

code> List< A> 可以仅引用列表 > A 。但是,列表< B> 实例不能保存 A 因此,不能向 listA 分配列表< A> 列出 listB 所指的 实例 >

或更一般地说: B A 表示 SomeGenericClass< B> SomeGenericClass< A> JLS§4.10






em> Java泛型教程,帮助我理解这一点:


http://java.sun.com/docs/books/tutorial/java/generics/subtyping.html


如果你想到有形的物体 - 你可以想象的东西 - >

  //一个笼子是一系列的东西,有条纹可以保存它们
interface Cage< E> extends Collection< E>
...
Cage< Lion> lionCage = ...;
Cage< Butterfly> butterflyCage = ...;

但是,动物笼呢?英语不明确,因此我们假设我们正在谈论全动物笼子

  Cage< Animal> animalCage = ...; 

这是一个笼子,用于容纳各种动物,混合在一起 。它必须有足够坚强的足够坚持在狮子,并且间隔足够紧密地握在蝴蝶。

...

因为狮子是一种动物(狮子是Animal的子类型),那么问题就变成:狮子笼是一种动物笼子吗? Cage ; Animal> ?。通过上述动物笼的定义,答案必须是否。这是惊人的!但是当你思考它时,它是完全有道理的:一只狮子笼不能假定保持在蝴蝶,蝴蝶笼不能被认为持有狮子。因此,这两个笼子都不能被视为全动物笼子:

  animalCage = lionCage; // compile-time error 
animalCage = butterflyCage; //编译时错误


I've a collection type:

Collection<A> collecA

And I've a list in my object:

List<B> listB

Where B is extending A

class B extends A { ... }

But I can't do the following:

collecA = listB

I can't understand why since Collection is implemented by List.

解决方案

Let's assume for a moment you could do what you describe:

class B extends A { ... }

Collection<A> collecA;
List<B> listB;

collecA = listB;  // normally an error, but lets pretend its allowed

collecA.add(new A()); // PROBLEM!

The method call collecA.add(new A()) appears okay since collecA is a collection that holds As. However, if the above assignment were allowed, then we have a problem becausecollecA is really reference to a List<B> instance - I just added an A into a list that can only hold Bs!

Asker also said:

I can't understand why since Collection is implemented by List.

It doesn't matter that Collection is a superclass of List. This assignment is illegal even if you used two lists.

class B extends A { ... }
List<A> listA;
List<B> listB;
listA = listB;  // still an error, still leads to the same problem

The key is that the List<A> variable can reference only Lists that can hold As. However, a List<B> instance cannot hold As. Therefore, a List<A> variable like listA cannot be assigned a reference to a List<B> instance referred to bylistB.

Or more generally speaking: B being a subclass of A does not imply that SomeGenericClass<B> is a subclass of SomeGenericClass<A> (JLS §4.10: Subtyping does not extend through generic types: T <: U does not imply that C<T> <: C<U>.)


It was this example/analogy from the Java Generics Tutorial that helped me understand this:

http://java.sun.com/docs/books/tutorial/java/generics/subtyping.html

"Understanding why becomes much easier if you think of tangible objects — things you can actually picture — such as a cage:

// A cage is a collection of things, with bars to keep them in.
interface Cage<E> extends Collection<E>;
...
Cage<Lion> lionCage = ...;
Cage<Butterfly> butterflyCage = ...;

But what about an "animal cage"? English is ambiguous, so to be precise let's assume we're talking about an "all-animal cage":

Cage<Animal> animalCage = ...;

This is a cage designed to hold all kinds of animals, mixed together. It must have bars strong enough to hold in the lions, and spaced closely enough to hold in the butterflies.
...
Since a lion is a kind of animal (Lion is a subtype of Animal), the question then becomes, "Is a lion cage a kind of animal cage? Is Cage<Lion> a subtype of Cage<Animal>?". By the above definition of animal cage, the answer must be "no". This is surprising! But it makes perfect sense when you think about it: A lion cage cannot be assumed to keep in butterflies, and a butterfly cage cannot be assumed to hold in lions. Therefore, neither cage can be considered an "all-animal" cage:

animalCage = lionCage;  // compile-time error
animalCage = butterflyCage; // compile-time error

"

这篇关于如何将继承对象的列表转换为Java中的对象集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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