scala 类型参数
用参数限定类型
泛型类
scala> class Student[T](val localId :T){
| def getSchoolId(hId :T)= "S-"+hId +"-"+localId
| }
defined class Student
scala> val tom = new Student[String]("uuuuuu")
tom: Student[String] = Student@7eb200ce
scala> tom.getSchoolId("222")
res1: String = S-222-uuuuuu
----------------------------------
泛型函数
scala> def getMiddle[T](a:Array[T]) = a(a.length / 2)
getMiddle: [T](a: Array[T])T
scala> getMiddle(Array("mary","had","a","cat"))
res2: String = a
本文介绍了Scala中的泛型概念,包括泛型类和泛型函数的使用方法。通过具体实例展示了如何定义泛型类并实现其方法,以及如何定义泛型函数来处理不同类型的数据。
1237

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



