1.Scala泛型类
Scala
object Test5 {
//格式
//定义:类名[T]
//使用:类名[类型]
abstract class A[T](){
val data : Array[T]
}
class A1 extends A [Int]{
override val data : Array[Int]=Array(1,2,3)
}
class A2 extends A[String]{
override val data=Array("a","b","c")
}
def main(args: Array[String]): Unit = {
val a = new A1()
val rs = a.data(1)
println(rs)
val b = new A2()
val rs1 = b.data(1)
println(rs1)
}
}
2.Scala泛型trait
Scala
//泛型trait
//定义格式:trait 特质名[泛型]
//使用格式:特质名[具体的类型]
object Test6 {
trait Logger[T]{
val name:T
def show(b:T):Unit ={
println(b)
}
}
class A extends Logger[String]{
override val name = "test"
}
def main(args: Array[String]): Unit = {
val a1 = new A()
a1.show("hi")
}
}