为什么A-> B不会使List< A> - > List< B>?这不会删除通配符的需要? [英] Why A->B doesn't make List<A>->List<B>? Wouldn't that remove need for wildcards?

查看:144
本文介绍了为什么A-> B不会使List< A> - > List< B>?这不会删除通配符的需要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:我不是一个专业的开发人员,我不打算成为一个。阅读关于Java的书,因为我想尝试Android编程,没有以前的Java经验。



我在阅读这本书 - 我更喜欢它。我已经阅读了关于泛型类的一部分,到了他们提到通配符的地方,并且困惑。



如果B扩展A:


  1. < A> 的子类型( c c>

  2. 列表< ;? extends B> List< ;?的子类型extends A>

后者允许编写接受通用类型参数的函数 - 例如列表< ;?扩展A> 。这样的函数将接受 List< B> List 的参数。



现在,对于我的问题:



用类似C ++的方式实现泛型 味道)?这将使得 List List 两个单独的类型,这将以期望的方式相关。这也允许简单地在函数中声明你期望参数是 List 的类型,这将允许 List< B>



我猜想有超过我们讨厌C ++,让我们的东西不同在这之后)也很有可能,我不知道的东西,使通配符是一个了不起的和有用的工具。如果您提到列表< X>

c>在您的答案中,请记住使用反引号,以避免将< X> 解释为HTML标记。



假设你有一个变量类型 List< A> code>。假设 List< B> 确实是 List< A> 的子类型。



这意味着这将是合法的:

  A>一个列表; 
a_list = new List< B>(); //当List< B>是列表< A>的子类型。
a_list.add(new A()); // 哇!

在我说的WOAH时,会发生以下情况:将一个类型A的项目添加到a_list。由于a_list被声明为 List< A> ,这应该是合法的。但是等待:a_list指向 List< B> 类型的东西。



现在我们将一个类型A添加到应该只存储类型B的项目的列表中,这显然不是我们想要的,因为A不是子类的B!


Disclaimer: I'm not a professional developer, and I'm not intending to become one. Reading book about Java, as I wanted to try Android programming, no previous Java experience whatsoever.

I'm reading this book - and I rather like it. I've read part of chapter about generic classes, got to the point where they mention wildcards, and got confused.

If B extends A:

  1. List<B> is not a subtype of List<A> (as I understand it they're exactly the same)
  2. List<? extends B> is a subtype of List<? extends A>

The latter allows for writing functions that accept arguments that are of generic type - for example List<? extends A>. Such function would accept an argument of either List<B> or List<A>.

Now, for my question:

Wouldn't it be simpler to implement generics in a manner similar to C++ (in a "template" flavour)? This would make List<B> and List<A> two separate types, that would be related in expected way. This would also allow to simply state in a function that you expect an argument to be of type List<A>, which would allow List<B> to fit there just fine.

I'm guessing there was more than "we hate C++, let's make things different" behind this :) It's also quite possible that I don't know something yet, that makes wildcards a fantastic and useful tool. What's your take on this?

Edit: if you're mentioning List<X> in your answer, remember to use backticks, to avoid <X> being interpreted as HTML tag.

解决方案

There's a simple reason.

Suppose you have a variable of type List<A>. Suppose List<B> was indeed a subtype of List<A>.

That means that when this would be legal:

List<A> a_list;
a_list = new List<B>(); //allowed when List<B> is subtype of list<A>
a_list.add(new A()); // WOAH!

Where I say WOAH, the following happens: You add an item of type A to a_list. Since a_list was declared as List<A>, this should be legal. But wait: a_list is pointing to something of type List<B>.

So now we add something of type A to a list that should store only items of type B, and this is clearly not what we want, since A is not a subclass of B!

这篇关于为什么A-&gt; B不会使List&lt; A&gt; - &gt; List&lt; B&gt;?这不会删除通配符的需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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