1. 泛型类
class Test1[T](val p1: T){
def t1():Unit = {
println("there is " + p1)
}
}
object Test {
def main(args: Array[String]): Unit = {
val t1 = new Test1("haha")
t1.t1()
}
}
在构造的Test1 类的时候, 传入一个参数,类型任意
2. 泛型方法
class Test1[T](val p1: T){
def t1[T](p2: T):Unit = {// 泛型方法
println("there is " + p1 + p2)
}
}
object Test {
def main(args: Array[String]): Unit = {
val t1 = new Test1("haha")
t1.t1("haha2")
}
}
3. 类型变量限定
[A <: B] : A 必须是B的子类
[A >: B] : A 必须是B的父类
[A <% B]: A 可以隐式转换为B
4. 多重限定
5. 协变
本文介绍了Scala中的泛型概念,包括泛型类、泛型方法的使用,并解释了类型变量限定的概念,如子类型限定[A<:B]、父类型限定[A>:B]及隐式转换限定[A<%B]。

被折叠的 条评论
为什么被折叠?



