C/C++教程

Scala(十一)--泛型

本文主要是介绍Scala(十一)--泛型,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

11.1 协变和逆变

  1)语法

class MyList[+T]{ //协变

}

class MyList[-T]{ //逆变

}

class MyList[T]{//不变

}

  2)说明

    协变:Son是Father的子类,则MyList[Son] 也作为MyList[Father]的“子类”。

    逆变:Son是Father的子类,则MyList[Son]作为MyList[Father]的“父类”。

    不变:Son是Father的子类,则MyList[Father]与MyList[Son]“无父子关系”。

  3)实操

package com.yuange.scala.day06

abstract class Animal{
  val name: String
}

abstract class Pet extends Animal{
}

class Dog extends Pet{
  override val name: String = "狗子"
}

class Cat extends Pet{
  override val name: String = "猫子"
}

class Lion extends Animal{
  override val name: String = "狮子"
}

class PetContainer[+P](val pet: P){
}

object TestGeneric {

  def testPrint(petContainer: PetContainer[Pet]) = {
    println(petContainer.pet.name)
  }

  def main(args: Array[String]): Unit = {
    val dog: Dog = new Dog()
    var cat: Cat = new Cat()
    var lion: Lion = new Lion()

    val dogContainer: PetContainer[Dog] = new PetContainer[Dog](dog)
    val catContainer: PetContainer[Cat] = new PetContainer[Cat](cat)

    println("dogContainer=" + dogContainer)
    println("catContainer=" + catContainer)
  }
}

11.2 泛型上下限

  1)语法

Class PersonList[T <: Person]{ //泛型上限

}

Class PersonList[T >: Person]{ //泛型下限

}

  2)泛型的上下限的作用是对传入的泛型进行限定。

  3)实操

package com.yuange.scala.day06

abstract class Animal{
  val name: String
}

abstract class Pet extends Animal{

}

class Dog extends Pet{
  override val name: String = "狗子"
}

class Cat extends Pet{
  override val name: String = "喵咪"
}

class Lion extends Animal{
  override val name: String = "狮子"
}

class PetContainer[P <: Pet](val pet: P){

}

object TestGenericTwo {
  def testPrint(petContainer: PetContainer[Pet]): Unit ={
    println(petContainer.pet.name)
  }

  def main(args: Array[String]): Unit = {
    var dog: Dog = new Dog()
    var cat: Cat = new Cat()
    var loin: Lion = new Lion()

    val dogContainer: PetContainer[Dog] = new PetContainer[Dog](dog)
    val catContainer: PetContainer[Cat] = new PetContainer[Cat](cat)
    val loinContainer: PetContainer[Lion] = new PetContainer[Lion](loin)  //error
    /*Error:(39, 24) type arguments [com.yuange.scala.day06.Lion] do not conform to class PetContainer's type parameter bounds [P <: com.yuange.scala.day06.Pet]
    val loinContainer: PetContainer[Lion] = new PetContainer[Lion](loin)
    Error:(39, 49) type arguments [com.yuange.scala.day06.Lion] do not conform to class PetContainer's type parameter bounds [P <: com.yuange.scala.day06.Pet]
    val loinContainer: PetContainer[Lion] = new PetContainer[Lion](loin)*/
  }
}

11.3 上下文限定

  1)语法

def f[A : B](a: A) = println(a) //等同于def f[A](a:A)(implicit arg:B[A])=println(a)

  2)上下问限定是将泛型和隐式转换的结合产物,以下两者功能相同,使用上下文限定[A : Ordering]之后,方法内无法使用隐式参数名调用隐式参数,需要通过implicitly[Ordering[A]]获取隐式变量。

  3)实操

def f[A:Ordering](a:A,b:A) =implicitly[Ordering[A]].compare(a,b)
def f[A](a: A, b: A)(implicit ord: Ordering[A]) = ord.compare(a, b)
这篇关于Scala(十一)--泛型的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!