泛类型

和java一样,scala也有泛型的概念,在scala里面泛型是使用方括号 [] 来接受类型参数的。通常使用字母A来作为参数标志符,当然你也可以使用其他任意的参数名称。

  1. class Stack[A] {
  2. private var elements: List[A] = Nil
  3. def push(x: A) { elements = x :: elements }
  4. def peek: A = elements.head
  5. def pop(): A = {
  6. val currentTop = peek
  7. elements = elements.tail
  8. currentTop
  9. }
  10. }

要使用一个泛类型,将一个具体的类型替换掉A即可。

  1. val stack = new Stack[Int]
  2. stack.push(1)
  3. stack.push(2)
  4. println(stack.pop) // prints 2
  5. println(stack.pop) // prints 1

上面的例子中,实例对象接收整型值,如果该类型有子类型,子类型也是可以传入的。

  1. class Fruit
  2. class Apple extends Fruit
  3. class Banana extends Fruit
  4. val stack = new Stack[Fruit]
  5. val apple = new Apple
  6. val banana = new Banana
  7. stack.push(apple)
  8. stack.push(banana)

变和逆变

1.语法

  1. class MyList[+T]{ //协变
  2. }
  3. class MyList[-T]{ //逆变
  4. }
  5. class MyList[T] //不变

2.说明
协变:Son是Father的子类,则MyList[Son] 也作为MyList[Father]的“子类”。
逆变:Son是Father的子类,则MyList[Son]作为MyList[Father]的“父类”。
不变:Son是Father的子类,则MyList[Father]与MyList[Son]“无父子关系”。

3.案例实操

  1. //泛型模板
  2. //class MyList<T>{}
  3. //不变
  4. //class MyList[T]{}
  5. //协变
  6. //class MyList[+T]{}
  7. //逆变
  8. //class MyList[-T]{}
  9. // 定义继承关系
  10. class Parent {}
  11. class Child extends Parent {}
  12. class SubChild extends Child {}
  13. // 定义带泛型的集合类型, 定义不同的逆变协边, 可以让 =号前后, 用父子类关系
  14. class MyCollection[-E] {}
  15. object Scala_TestGeneric {
  16. def main(args: Array[String]): Unit = {
  17. // 1. 协变和逆变
  18. val child: Parent = new Child
  19. // val childList: MyCollection[Parent] = new MyCollection[Child]
  20. val childList: MyCollection[SubChild] = new MyCollection[Child]
  21. // 2. 上下限
  22. def test[A <: Child](a: A): Unit = {
  23. println(a.getClass.getName)
  24. }
  25. test[SubChild](new SubChild)
  26. }
  27. }

泛型上下限

1.语法

  1. Class PersonList[T <: Person]{ //泛型上限, 传的类型可以是Person和他的子类
  2. }
  3. Class PersonList[T >: Person]{ //泛型下限, 传的类型可以是Person和他的父类
  4. }

2.说明
泛型的上下限的作用是对传入的泛型进行限定

3.案例实操

  1. class Parent {}
  2. class Child extends Parent {}
  3. class SubChild extends Child {}
  4. object Scala_TestGeneric {
  5. def main(args: Array[String]): Unit = {
  6. //test(classOf[SubChild])
  7. //test[Child](new SubChild)
  8. }
  9. //泛型通配符之上限
  10. //def test[A <: Child](a:Class[A]): Unit ={
  11. // println(a)
  12. //}
  13. //泛型通配符之下限
  14. //def test[A >: Child](a:Class[A]): Unit ={
  15. // println(a)
  16. //}
  17. //泛型通配符之下限 形式扩展
  18. def test[A >: Child](a: A): Unit = {
  19. println(a.getClass.getName)
  20. }
  21. }

上下文限定

1.语法

  1. //A是一个泛型, B是隐式参数的泛型名称而B这个泛型的名称里面的泛型又是A, 用这个隐式参数用implicitly
  2. def f[A : B](a: A) = println(a)
  3. //等同于
  4. def f[A](a:A)(implicit arg:B[A])=println(a)

2.说明
上下文限定是将泛型和隐式转换的结合产物,以下两者功能相同,使用上下文限定 [A : Ordering] 之后,方法内无法使用隐式参数名调用隐式参数,需要通过implicitly[Ordering[A]] 获取隐式变量,如果此时无法查找到对应类型的隐式变量,会发生出错误

  1. implicit val x = 1
  2. val y = implicitly[Int]
  3. val z = implicitly[Double]

3.案例实操

  1. def f[A:Ordering](a:A,b:A) =implicitly[Ordering[A]].compare(a,b)
  2. def f[A](a: A, b: A)(implicit ord: Ordering[A]) = ord.compare(a, b)