//实例化之后使用get方法必须传入相同类型的参数 class A[T](x: T) { def get(x: T) { print(x) } } var a1 = new A(1) a1.get(1) //上边界泛型实现 class Persion(name: String) { def prn = println("name:" + name) def get(p: Persion) { prn p.prn } } class Worker(name: String) extends Persion(name) { var get1 = println("函数") } def process[T <: Worker](p: T, p1: T) { //[T <: Worker]要求必须在worker自己以及子类中 p.get(p1) } def process1[T <: Persion](p: T, p1: T) { //[T <: Persion]要求必须在Persion自己以及子类中 p.get(p1) } def process3[T >: Persion](p: T) { //[T >: Persion]要求必须在Persion自己以及继承的父类中 (p.asInstanceOf[Persion]).prn if (p.getClass == classOf[Persion]) { print("Persion!") } else if (p.getClass == classOf[Worker]) { print("Worker!") } else { print("未匹配!") } } var p1 = new Worker("abc") var p2 = new Worker("mnb") process(p1, p2) var p3 = new Persion("ab1c") var p4 = new Persion("mn1b") process1(p3, p4) var p5 = new Persion("a8899") var p6 = new Worker("a8899!!") process3(p5) process3(p6)