简体   繁体   English

公众有什么区别 <T extends Animal> void addAll(List <T> 动物)和public void addAll(List <Animal> 动物)?

[英]What is the difference between public <T extends Animal> void addAll(List<T> animals) and public void addAll(List<Animal> animals)?

I am a bit confused with this, so a bit of clarification would be appreciated. 我对此感到有点困惑,所以我会对此表示赞赏。

public <T extends Animal> void addAll(List<T> animals)

vs.

public void addAll(List<Animal> animals)

The difference is in which type parameters in the List will be accepted by the method. 不同之处在于该方法将接受List中的哪些类型参数。

In the first method, T can be Animal or any subclass, so addAll will accept a List<Animal> , a List<Dog> , or a List<Cat> . 在第一种方法中, T可以是Animal或任何子类,因此addAll将接受List<Animal>List<Dog>List<Cat> Note that this signature is equivalent to 请注意,此签名相当于

public void addAll(List<? extends Animal> animals)

when you don't need the exact type of Animal in the body of the method. 当你不需要方法体中确切类型的Animal时。

In the second method, you've specified that the type parameter must be Animal . 在第二种方法中,您已指定type参数必须为Animal Java's generics are invariant, so no subtypes of Animal will be allowed. Java的泛型是不变的,因此不允许使用Animal子类型。 This method will accept a List<Animal> , but not a List<Dog> or a List<Cat> . 此方法将接受List<Animal> ,但不接受List<Dog>List<Cat>

in simple words 'T' is an object type. 简单来说,'T'是一种对象类型。 So List 'T" type can accept any type of object whereas List of type Animal will take elements of only Animal type and not any other object. 所以List'T'类型可以接受任何类型的对象,而Animal类型的List将只接受Animal类型的元素,而不接受任何其他对象。

That's the advantage of using Generics. 这是使用泛型的优势。

暂无
暂无

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

相关问题 为什么不能列出<!--? extends Animal-->替换为列表<animal> ?</animal> - Why can't List<? extends Animal> be replaced with List<Animal>? 泛型:列表<? extends Animal>与列表相同<Animal> ? - Generics : List<? extends Animal> is same as List<Animal>? 为什么什么时候<t extends animal>选角名单<list<animal> > 列出<list<t> > 导致编译错误,但转换列表<animal>列出<t>引起警告? </t></animal></list<t></list<animal></t> - Why when <T extends Animal> casting List<List<Animal>> to List<List<T>> causes compilation error, but casting List<Animal> to List<T> causes warning? 在列表上使用addAll() <? extends E> a和b不起作用 - Using addAll() on List<? extends E> a and b doesn't work 之间的区别 <T extends A> void foo(T t)和void foo(A a) - Difference between <T extends A> void foo(T t) and void foo(A a) 为什么我不能创建一个类? 我正在尝试使用 Animal 数组来容纳不同的动物 - Any reason why I can't create a class? I'm trying to have a Animal array to hold different animals Java列表/包含各种动物的动物阵列 - Java list/array of animals containing a variety of animals 在没有排序的情况下找到重量最高的动物()(地图<string, animal> (Where Animal - 类))</string,> - Find animals with the highest weight without sorted() (map<String, Animal> (Where Animal - class)) java中的赋值运算符和addAll有什么区别? - What is the difference between assignment operator and addAll in java? 什么是`public static <T> void main(String [] args)`代表什么? - What does `public static <T> void main(String[] args)` stand for?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM