简体   繁体   English

scala 类型系统 - 了解具有下限/上限的协变

[英]scala type system - understanding covariant with lower/upper bounds

I am new to scala and have questions on scala generics我是 Scala 的新手,对 Scala 泛型有疑问

在此处输入图片说明

Question is how animalContainer.add method is accepting new Cat.问题是animalContainer.add 方法如何接受新的Cat。 From my understanding B>:A - A is Animal and B is Cat.根据我的理解 B>:A - A 是动物,B 是猫。 Cat is NOT super typs of animal .猫不是动物的超级类型。 How it is working..它是如何工作的..

B is not Cat, B is Animal. B不是猫,B是动物。 The inferred type of add is then effectively:然后推断出的 add 类型是:

def add(element: Animal)

You are able to pass Cat here because Cat extends Animal.您可以在这里传递 Cat,因为 Cat 扩展了 Animal。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM