类型上界
在Scala中,类型参数和抽象类型都可以有一个类型边界约束。这种类型边界在限制类型变量实际取值的同时还能展露类型成员的更多信息。比如像T <: A这样声明的类型上界表示类型变量T应该是类型A的子类。下面的例子展示了类PetContainer的一个类型参数的类型上界。
abstract class Animal {def name: String}abstract class Pet extends Animal {}class Cat extends Pet {override def name: String = "Cat"}class Dog extends Pet {override def name: String = "Dog"}class Lion extends Animal {override def name: String = "Lion"}class PetContainer[P <: Pet](p: P) {def pet: P = p}val dogContainer = new PetContainer[Dog](new Dog)val catContainer = new PetContainer[Cat](new Cat)
// this would not compileval lionContainer = new PetContainer[Lion](new Lion)
类PetContainer接受一个必须是Pet子类的类型参数P。因为Dog和Cat都是Pet的子类,所以可以构造PetContainer[Dog]和PetContainer[Cat]。但在尝试构造PetContainer[Lion]的时候会得到下面的错误信息:
type arguments [Lion] do not conform to class PetContainer's type parameter bounds [P <: Pet]
这是因为Lion并不是Pet的子类。
类型下界
trait Node[+B] {def prepend(elem: B): Node[B]}case class ListNode[+B](h: B, t: Node[B]) extends Node[B] {def prepend(elem: B): ListNode[B] = ListNode(elem, this)def head: B = hdef tail: Node[B] = t}case class Nil[+B]() extends Node[B] {def prepend(elem: B): ListNode[B] = ListNode(elem, this)}
trait Node[+B] {def prepend[U >: B](elem: U): Node[U]}case class ListNode[+B](h: B, t: Node[B]) extends Node[B] {def prepend[U >: B](elem: U): ListNode[U] = ListNode(elem, this)def head: B = hdef tail: Node[B] = t}case class Nil[+B]() extends Node[B] {def prepend[U >: B](elem: U): ListNode[U] = ListNode(elem, this)}
trait Birdcase class AfricanSwallow() extends Birdcase class EuropeanSwallow() extends Birdval africanSwallowList= ListNode[AfricanSwallow](AfricanSwallow(), Nil())val birdList: Node[Bird] = africanSwallowListbirdList.prepend(EuropeanSwallow())





