简体   繁体   English

狗是动物,但清单 <Dog> 没有列出 <Animal> 。 如何在泛型/多态函数中安全使用它?

[英]Dog is Animal but list<Dog> is not list<Animal>. How to use it safely in a generic/polymorphic function?

It is well-known that a Dog is an Animal but List<Dog> is not List<Animal> ( SO question ) 众所周知, DogAnimalList<Dog>不是List<Animal> (因此, 问题

Anyway, if we have a function 无论如何,如果我们有一个功能

void f(Animal a1, Animal a2); // Java

void f(Animal * a1, Animal * a2) // C++

We can safely use f() passing in objects of type Dog / Dog* 我们可以安全地使用f()传入Dog / Dog*类型的对象

Now suppose, we want to generalize it as if it has many arguments of type Animal by using a List of them (and not specifically a multiple argument list ... ). 现在假设,我们想通过使用它们的List来概括它,使其具有许多Animal类型的参数(而不是专门针对多参数列表...)。 The List is not going to be modified by the function, adding a Cat to a List<Dog> for instance. List不会被该函数修改,例如,将Cat添加到List<Dog>中。

This: 这个:

void fg (List<Animal>); // Java
void fg (List<Animal *>) // C++

is not going to allow a call passing in a List<Dog> / List<Dog*> value. 不允许呼叫传递List<Dog> / List<Dog*>值。

What is the way to go? 怎么走?

May be in Java 可能在Java中

void fg (List<? extends Animal>)

and in C++ 和在C ++中

template<T>
void fg (List<T>)

Is that ok? 这可以吗?
Are there other alternatives? 还有其他选择吗?

Thanks. 谢谢。

The list is not going to be modified by the function [...]. 该列表将不会通过功能[...]进行修改。 What is the way to go? 怎么走?

In Java, declare a parameter of type List<? extends Animal> 在Java中,声明类型为List<? extends Animal>的参数List<? extends Animal> List<? extends Animal> . List<? extends Animal> This provides a wildcard bounded by Animal type argument to the List generic parameter. 这提供了以Animal类型参数为界的通配符,该通配符与List通用参数有关。 You can read that as a list of any unknown type that extends Animal . 您可以将其作为扩展Animal的任何未知类型列表来阅读。 The fact that it is an unknown type means that the only type that can be used wherever a value of the List 's type parameter T is expected is the null type, whose only value is null . 这是一个未知类型这一事实意味着,无论何时期望List的类型参数T的值都可以使用的唯一类型是null类型,其唯一值为null

A List<Dog> is a type that fits that description, so is a List<Cat> . List<Dog>是适合该描述的类型, List<Cat>

暂无
暂无

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

相关问题 将List <Animal>转换为List <Dog> - Casting List<Animal> to List<Dog> 是列表<dog>列表的子类<animal> ? 为什么 Java generics 不是隐式多态的?</animal></dog> - Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic? 是否可以在没有演员的情况下从 Animal 列表中获取 Dog 列表? - Is it possible to get a list of Dog from a list of Animal without a cast? 是否可以使用类型参数 List 覆盖继承的方法<animal>与列表<dog> ?</dog></animal> - Is it possible to override an inherited method with type parameter List<Animal> with List<Dog>? 类型不匹配:无法从地图转换 <String,List<Animal> &gt;到地图 <String,List<Dog> &gt; - Type mismatch: cannot convert from Map<String,List<Animal>> to Map<String,List<Dog>> java对象,我用动物speedy = new dog()创建动物或狗吗? 为什么? - java objects, do I create an animal or a dog with animal speedy = new dog(); and why? 泛型:列表<? extends Animal>与列表相同<Animal> ? - Generics : List<? extends Animal> is same as List<Animal>? 编写一个 Dog 构造函数,它有一个参数,即名称,并调用超级构造函数并传递名称和动物类型“狗”。 不能通过类型 - Write a Dog constructor that has one argument, the name, and calls the super constructor passing it the name and the animal type “dog”. cant pass type 为什么不能列出<!--? extends Animal-->替换为列表<animal> ?</animal> - Why can't List<? extends Animal> be replaced with List<Animal>? 如何搜索我的数组列表以查找保留的动物? - How do I search my array list to find a reserved animal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM