scala泛型下界

object D02_泛型下界 {

  class Person

  class PoliceMen extends Person

  class SuperMen extends PoliceMen

  // 只能接收PoliceMen或者其父类型
  def demo[T >: PoliceMen](arr: Array[T]) = println(arr)

  def main(args: Array[String]): Unit = {
    demo(Array(new Person, new Person))
    demo(Array(new PoliceMen, new PoliceMen))

    // 运行会报错
    // demo(Array(new SuperMen, new SuperMen))
  }

}

你可能感兴趣的:(scala,开发语言,后端)