引言
**什么示泛型:**其实意思与Java的泛型是一样的,也是定义一种类型参数,比如在集合,在类,在函数中,定义类型参数,然后就可以保证使用到该类型参数的地方,就肯定,也只能是这种类型。从而实现程序更好的健壮性。
泛型类
- 泛型类,顾名思义,其实就是在类的声明中,定义一些泛型类型,然后在类内部,比如field(字段)或者method(方法),就可以使用这些泛型类型。
- 使用泛型类,通常是需要对类中的某些成员,比如某些field和method中的参数或变量,进行统一的类型限制,这样可以保证程序更好的健壮性和稳定性。
- 如果不使用泛型进行统一的类型限制,那么在后期程序运行过程中,难免会出现问题,比如传入了不希望的类型,导致程序出问题。
- 在使用类的时候,比如创建类的对象,将类型参数替换为实际的类型,即可。
- Scala自动推断泛型类型特性:直接给使用了泛型类型的field赋值时,Scala会自动进行类型推断。
class Student17_1[T](val locald:T) {
def getSchool(hukould:T) = "S-" + hukould + "-" + locald
}
object Textaa {
def main(args: Array[String]): Unit = {
//--------------17---------1------泛型类
val leo = new Student17_1[Int](111)
println(leo.getSchool(123))
//S-123-111
}
}
泛型函数
- 泛型函数,与泛型类类似,可以给某个函数在声明时指定泛型类型,然后在函数体内,多个变量或者返回值之间,就可以使用泛型类型进行声明,从而对某个特殊的变量,或者多个变量,进行强制性的类型限制。
- 与泛型类一样,你可以通过给使用了泛型类型的变量传递值来让Scala自动推断泛型的实际类型,也可以在调用函数时,手动指定泛型类型。
object Textaa {
def main(args: Array[String]): Unit = {
//-------------17---------2-----泛型函数
def getCard[T](content: T) = {
if(content.isInstanceOf[Int]) "int card, " + content
else if(content.isInstanceOf[String]) "string card , " + content
else "card: " + content
}
print(getCard("Hello word")) //string card , Hello word
println(getCard(123)) //int card, 123
}
}
//还可以用模式匹配写
def getCard_17_2_case[T](content:T): Unit ={
content match {
case a1:Int => println(content + "int card")
case a2:String => println(content + "String card")
case _ :Any => println("any card")
}
}
getCard_17_2_case(123) //123 int card
上边界
在指定泛型类型的时候,有时,我们需要对泛型类型的范围进行界定,而不是可以是任意的类型。比如,我们可能要求某个泛型类型,它就必须是某个类的子类,这样在程序中就可以放心地调用泛型类型继承的父类的方法,程序才能正常的使用和运行。此时就可以使用上下边界Bounds的特性。
Scala的上下边界特性允许泛型类型必须是某个类的子类,或者必须是某个类的父类
上边界:泛型类型是某个类的子类或本身这个类
下边界:泛型类型是某个类的父类或本身这个类
class Persion_17_3_1(val name : String) {
def sayHello = println("Hello,i'm " + name)
def makeFriends(p :Persion_17_3_1): Unit = {
sayHello
p.sayHello
}
}
class Student_17_3_2(name :String) extends Persion_17_3_1 (name) {
}
class Party[T <: Persion_17_3_1](p1: T, p2: T) {
def play = p1.makeFriends(p2)
}
object Textaa {
def main(args: Array[String]): Unit = {
//-------------17---------2----- 上边界Bounds
val s1 = new Student_17_3_2("s1")
val s2 = new Student_17_3_2("s2")
val p1 = new Persion_17_3_1("p1")
val p2 = new Persion_17_3_1("p2")
val party1 = new Party[Student_17_3_2](s1,s2)
//party1.play
val party2 = new Party[Persion_17_3_1](p1,p2)
party2.play
}
}
下边界
class Father_17_4_1(val name :String) {
}
class Child_17_4_2(name :String) extends Father_17_4_1(name) {
}
//-------------17---------4----- 下边界Bounds
def getIDCard_17_4[R >: Child_17_4_2](a:R): Unit ={
if (a.getClass == classOf[Child_17_4_2]) println("please tell us your parents' names.")
else if (a.getClass == classOf[Father_17_4_1]) println("sign your name for your child's id card.")
else println("sorry, you are not allowed to get id card.")
}
def getIDCard_17_4_case[B >: Child_17_4_2](f:B): Unit ={
f match {
case a1 : Child_17_4_2 => println("please tell us your parents' names.")
case a2 : Father_17_4_1 => println("sign your name for your child's id card.")
case _ : Any => println("sorry, you are not allowed to get id card.")
}
}
val c1 = new Child_17_4_2("c1")
val f1 = new Father_17_4_1("f1")
//getIDCard_17_4(f1) //sign your name for your child's id card.
getIDCard_17_4_case(c1) //please tell us your parents' names.
}
}