scala中的多态 Ad-hoc polymorphism和type class
多态的类型(polymorphism)(1) parametric多态下面例子来自scalaz教程:scala> def head[A](xs: List[A]): A = xs(0)head: [A](xs: List[A])Ascala> head(1 :: 2 :: Nil)res0: Int = 1scala> case class Car(make: String)defined class Carscala> head(Car("Civic") :: Car("CR-V") :: Nil)res1: Car = Car(Civic)可以看出参数多态与类型无关。2 sub-type 多态OOP中子类重写父类方法,父类引用指向子类。3 ad-hoc 多态Ad-hoc polymorp...阅读全文