泛型

[B < : A] UpperBound 上界:B类型的上界是A类型,即B类型的父类是A类型

[B>: A] LowerBound 下界:B类型的下界是A类型,即B类型的子类是A类型

[B <% A] ViewBound 表示B类型要转换成A类型,需要一个隐式转换函数

[B : A ] ContextBound 需要一个隐式转换的值

[-A , +B ]

[-A] 逆变,作为参数类型,如果A是T的子类,那么C[T]是C[A]的子类

[B] 协变 作为返回类型,如果B是T的子类,那么C[A]是C[T]的子类

UpperBound

UpperBoundDemo.scala
package day04

/**
  * 上界 UpperBound
  */
class UpperBuoudDemo[T <: Comparable[T]] {
  def select(first: T,second: T): T = {
    if (first.compareTo(second) > 0 ) first else second
  }
}

object UpperBuoudDemo {
  def main(args: Array[String]): Unit = {

    val u = new UpperBuoudDemo[MissRight]

    val m1 = new MissRight("biaozi",120)
    val m2 = new MissRight("erdiao",465)

    val res = u.select(m1,m2)

    println(res.name)
  }
}

class MissRight(val name: String,val faceValue: Int) extends Comparable[MissRight] {
  override def compareTo(o: MissRight): Int = {
    this.faceValue - o.faceValue
  }
}

ViewBound

ViewBoundDemo.scala
package day04

/**
  * [B <% A] ViewBound
  */
class ViewBoundDemo[T <% Ordered[T]] {
  def select(first: T,second: T): T = {
    if (first > second) first else second
  }
}

object ViewBoundDemo {
  def main(args: Array[String]): Unit = {

    import MyPredef.selectGirl

    val viewBoundDemo = new ViewBoundDemo[Girl]

    val g1 = new Girl("biaozi",26,120)
    val g2 = new Girl("erdiao",24,120)

    val res = viewBoundDemo.select(g1,g2)

    println(res.name)
  }
}
Girl.scala
package day04

class Girl(val name: String, val age: Int, val faceValue: Int) {

}

MyPredef.scala
package day04

object MyPredef {
  implicit def fileToRichFile(file: String) = new RichFile(file)

  implicit val selectGirl = (g: Girl) => new Ordered[Girl]{
    override def compare(that: Girl): Int = {
      if (g.faceValue == that.faceValue){
        that.age - g.age
      }else {
        g.faceValue - that.faceValue
      }
    }
  }
}

contextbound

ContextBoundDemo.scala
package day04

/**
  * [B : A] ContextBound
  */
class ContextBoundDemo[T: Ordering] {
  def select(first: T, second: T): T = {
    val ord: Ordering[T] = implicitly[Ordering[T]]
    if(ord.gt(first,second)) first else second
  }
}
object ContextBoundDemo {
  def main(args: Array[String]): Unit = {

    import MyPredef.OrderingGirl

    val contextBoundDemo = new ContextBoundDemo[Girl]

    val g1 = new Girl("shabi",51,21)
    val g2 = new Girl("chunhuo",45,21)

    val res = contextBoundDemo.select(g1,g2)

    println(res.name)
  }
}
MyPredef.scala
package day04

object MyPredef {
  implicit def fileToRichFile(file: String) = new RichFile(file)

  implicit val selectGirl = (g: Girl) => new Ordered[Girl]{
    override def compare(that: Girl): Int = {
      if (g.faceValue == that.faceValue){
        that.age - g.age
      }else {
        g.faceValue - that.faceValue
      }
    }
  }
  implicit object OrderingGirl extends Ordering[Girl]{
    override def compare(x: Girl, y: Girl): Int = {
      if (x.faceValue == y.faceValue){
        y.age - x.age
      }else {
        x.faceValue - y.faceValue
      }
    }
  }
}

你可能感兴趣的:(泛型)