Spark基础-scala学习(七类型参数)
Posted sky-chen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spark基础-scala学习(七类型参数)相关的知识,希望对你有一定的参考价值。
类型参数是什么
- 类似于java泛型,泛型类
- 泛型函数
- 上边界Bounds
- 下边界
- View Bounds
- Context Bounds
- Manifest Context Bounds
- 协变和逆变
- Existential Type
泛型类
scala> :paste
// Entering paste mode (ctrl-D to finish)
class Student[T](val localId:T){
def getSchoolId(hukouId:T) = "S-"+hukouId+"-"+localId
}
// Exiting paste mode, now interpreting.
defined class Student
scala> val s = new Student[Int](11)
s: Student[Int] = [email protected]
scala> s.getSchoolId(234)
res1: String = S-234-11
泛型函数
- 泛型函数,与泛型类类似,可以给某个函数在声明时指定泛型类型,然后在函数体内,多个变量或者返回值之间,就可以使用泛型类型进行声明,从而对某个特殊的变量,或者多个变量,进行强制性的类型限制
- 与泛型类一样,你可以通过使用了泛型类型的变量传递值来让Scala自动推断泛型的实际类型,也可以在调用函数时,手动指定泛型类型
scala> :paste
// Entering paste mode (ctrl-D to finish)
def getCard[T](content:T)={
if(content.isInstanceOf[Int]) "card: 001,"+content
else if(content.isInstanceOf[String]) "card:this is your card, "+content
else "card: "+content
}
// Exiting paste mode, now interpreting.
getCard: [T](content: T)String
scala> getCard[String]("leo")
res2: String = card:this is your card, leo
scala> getCard[Int](123)
res3: String = card: 001,123
上边界Bounds
- 在指定泛型类型的时候,有时,我们需要对泛型类型的范围进行界定,而不是可以是任意的类型。比如,我们可能要求某个泛型类型,他就必须是某个类的子类,这样在程序中就可以放心地调用泛型类型继承的父类的方法,程序才能正常的使用和运行。此时就可以使用上下边界Bounds的特性
- scala的上下边界特性允许泛型类型必须是某个类的子类,或者必须是某个类的父类
scala> :paste
// Entering paste mode (ctrl-D to finish)
class Person(val name:String){
def sayHello = println("Hello,I‘m "+name)
def makeFriends(p:Person){
sayHello
p.sayHello
}
}
class Student(name:String) extends Person(name)
class Party[T <: Person](p1:T,p2:T){
def play = p1.makeFriends(p2)
}
// Exiting paste mode, now interpreting.
defined class Person
defined class Student
defined class Party
scala> val p = new Person("Tom")
p: Person = [email protected]
scala> val p2 = new Person("leo")
p2: Person = [email protected]
scala> p.makeFriends(p2)
Hello,I‘m Tom
Hello,I‘m leo
scala> val s1 = new Student("Jarry")
s1: Student = [email protected]
scala> val s2 = new Student("Marry")
s2: Student = [email protected]
scala> val pa = new Party[Student](s1,s2)
pa: Party[Student] = [email protected]
scala> pa.play
Hello,I‘m Jarry
Hello,I‘m Marry
下边界Bounds
- 除了指定泛型类型的上边界,还可以指定下边界,即指定泛型类型必须是某个类的父类
scala> :paste
// Entering paste mode (ctrl-D to finish)
class Father(val name:String)
class Child(name:String) extends Father(name)
def getIDCard[R >: Child](person:R){
if(person.getClass == classOf[Child]) println("please tell us your parents‘ names.")
else if(person.getClass == classOf[Father]) println("sign your name for your child‘s id card.")
else println("sorry,you are not allowed to get id card.")
}
// Exiting paste mode, now interpreting.
defined class Father
defined class Child
getIDCard: [R >: Child](person: R)Unit
scala> val f = new Father("fa")
f: Father = [email protected]
scala> val c = new Child("cd")
c: Child = [email protected]
scala> getIDCard[Father](f)
sign your name for your child‘s id card.
scala> getIDCard[Child](c)
please tell us your parents‘ names.
View Bounds
- 上下边界Bounds,虽然可以让一种泛型类型,支持有父子关系的多种类型。但是,在某个类与上下边界Bounds指定的父子类型范围内的类都没有任何关系,则默认是肯定不能接受的
- 然而,View Bounds作为一种上下边界Bounds的加强版,支持可以对类型进行隐式转换,将指定的类型进行隐式转换后,再判断是否在边界指定的类型范围内
- 案例:跟小狗交朋友
scala> :paste
// Entering paste mode (ctrl-D to finish)
class Person(val name:String){
def sayHello = println("Hello,I‘m "+name)
def makeFriends(p:Person){
sayHello
p.sayHello
}
}
class Student(name:String) extends Person(name)
class Dog(val name:String){def sayHello = println("Wang,Wang,I‘m "+name)}
implicit def dog2person(dog: Object):Person = if(dog.isInstanceOf[Dog]){val _dog = dog.asInstanceOf[Dog];new Person(_dog.name)} else Nil
class Party[T <% Person](p1:T,p2:T)
// Exiting paste mode, now interpreting.
<pastie>:23: warning: implicit conversion method dog2person should be enabled
by making the implicit value scala.language.implicitConversions visible.
This can be achieved by adding the import clause ‘import scala.language.implicitConversions‘
or by setting the compiler option -language:implicitConversions.
See the Scaladoc for value scala.language.implicitConversions for a discussion
why the feature should be explicitly enabled.
implicit def dog2person(dog: Object):Person = if(dog.isInstanceOf[Dog]){val _dog = dog.asInstanceOf[Dog];new Person(_dog.name)} else Nil
^
defined class Person
defined class Student
defined class Dog
dog2person: (dog: Object)Person
defined class Party
scala> val leo = new Student("leo")
leo: Student = [email protected]
scala> val doggy = new Dog("doggy")
doggy: Dog = [email protected]
scala> val party = new Party(leo,doggy)
party: Party[Object] = [email protected]
Context Bounds
- Context Bounds是一种特殊的Bounds,它会根据泛型类型的声明,比如“T:类型”要求必须存在一个类型为“类型[T]”的隐式值。其实个人认为,Context Bounds之所以叫做Context,是因为它基于的是一种全局的上下文,需要使用到上下文中的隐式值以及注入
- 案例:使用Scala内置的比较器比较大小
scala> :paste
// Entering paste mode (ctrl-D to finish)
class Calculator[T:Ordering](val number1:T,val number2:T){
def max(implicit order:Ordering[T]) = if(order.compare(number1,number2)>0)number1 else number2
}
// Exiting paste mode, now interpreting.
defined class Calculator
scala> val ca = new Calculator[Int](12,23)
ca: Calculator[Int] = [email protected]
scala> ca.max
res8: Int = 23
Manifest Context Bounds
- 在scala中,如果要实例化一个泛型数组,就必须使用Manifest Context Bounds,也就是说,如果数组元素类型为T的话,需要为类或者函数定义[T:Manifest]泛型类型,这样才能实例化Array[T]这种泛型数组
- 案例:打包饭菜(一种食品打成一包)
scala> :paste
// Entering paste mode (ctrl-D to finish)
class Meat(val name:String)
class Vegetable(val name:String)
def packageFood[T:Manifest](food: T*) = {
val foodPackage = new Array[T](food.length)
for(i <- 0 until food.length) foodPackage(i) = food(i)
foodPackage
}
// Exiting paste mode, now interpreting.
defined class Meat
defined class Vegetable
packageFood: [T](food: T*)(implicit evidence$1: Manifest[T])Array[T]
scala> val gongbaojiding = new Meat("gongbaojiding")
gongbaojiding: Meat = [email protected]
scala> val shoushibaocai = new Meat("shoushibaocai")
shoushibaocai: Meat = [email protected]
scala> val meatPackage = packageFood(gongbaojiding,shoushibaocai)
meatPackage: Array[Meat] = Array([email protected], [email protected])
协变和逆变
- scala的协变和逆变完全解决了java中的泛型的一大缺憾
- 举例来说,java中,如果有professional是Master的子类,那么Card(Professionnal)是不是Card(Master)的子类呢?答案是:不是。
- 而scala中,只要灵活使用协变和逆变,就可以解决java泛型的问题
- 案例:进入会场
scala> :paste
// Entering paste mode (ctrl-D to finish)
class Master
class Professional extends Master
//大师以及大师级别以下的名片都可以进入会场
class Card[+T](val name:String)
def enterMeet(card:Card[Master]){
println("Welcome to have this meeting")
}
// Exiting paste mode, now interpreting.
defined class Master
defined class Professional
defined class Card
enterMeet: (card: Card[Master])Unit
scala> val leo = new Card[Master]("leo")
leo: Card[Master] = [email protected]
scala> val jack = new Card[Professional]("jack")
jack: Card[Professional] = [email protected]
scala> enterMeet(leo)
Welcome to have this meeting
scala> enterMeet(jack)
Welcome to have this meeting
scala> :paste
// Entering paste mode (ctrl-D to finish)
class Card[-T](val name:String)
def enterMeet(card:Card[Professional]){
println("welcome to have this meeting!")
}
// Exiting paste mode, now interpreting.
defined class Card
enterMeet: (card: Card[Professional])Unit
scala> val leo = new Card[Master]("leo")
leo: Card[Master] = [email protected]
scala> val jack = new Card[Professional]("jack")
jack: Card[Professional] = [email protected]
scala> enterMeet(leo)
welcome to have this meeting!
scala> enterMeet(jack)
welcome to have this meeting!
Existential Type
- 在scala中,有一种特殊的类型参数,就是Existential Type存在性类型。
Array[T] forSome {type T}
//占位符
Array[_]
以上是关于Spark基础-scala学习(七类型参数)的主要内容,如果未能解决你的问题,请参考以下文章