码迷,mamicode.com
首页 > 其他好文 > 详细

Spark基础-scala学习(七、类型参数)

时间:2018-12-17 02:16:24      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:sorry   不能   extends   import   灵活   泛型数组   define   length   多个   

类型参数是什么

  1. 类似于java泛型,泛型类
  2. 泛型函数
  3. 上边界Bounds
  4. 下边界
  5. View Bounds
  6. Context Bounds
  7. Manifest Context Bounds
  8. 协变和逆变
  9. 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] = Student@a451491

scala> s.getSchoolId(234)
res1: String = S-234-11

泛型函数

  1. 泛型函数,与泛型类类似,可以给某个函数在声明时指定泛型类型,然后在函数体内,多个变量或者返回值之间,就可以使用泛型类型进行声明,从而对某个特殊的变量,或者多个变量,进行强制性的类型限制
  2. 与泛型类一样,你可以通过使用了泛型类型的变量传递值来让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

  1. 在指定泛型类型的时候,有时,我们需要对泛型类型的范围进行界定,而不是可以是任意的类型。比如,我们可能要求某个泛型类型,他就必须是某个类的子类,这样在程序中就可以放心地调用泛型类型继承的父类的方法,程序才能正常的使用和运行。此时就可以使用上下边界Bounds的特性
  2. 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 = Person@e344f14

scala> val p2 = new Person("leo")
p2: Person = Person@6e96f3cd

scala> p.makeFriends(p2)
Hello,I‘m Tom
Hello,I‘m leo

scala> val s1 = new Student("Jarry")
s1: Student = Student@58ca1cad

scala> val s2 = new Student("Marry")
s2: Student = Student@64c79b69

scala> val pa = new Party[Student](s1,s2)
pa: Party[Student] = Party@6a28d987

scala> pa.play
Hello,I‘m Jarry
Hello,I‘m Marry

下边界Bounds

  1. 除了指定泛型类型的上边界,还可以指定下边界,即指定泛型类型必须是某个类的父类
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 = Father@40f40221

scala> val c = new Child("cd")
c: Child = Child@3d44f15d

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

  1. 上下边界Bounds,虽然可以让一种泛型类型,支持有父子关系的多种类型。但是,在某个类与上下边界Bounds指定的父子类型范围内的类都没有任何关系,则默认是肯定不能接受的
  2. 然而,View Bounds作为一种上下边界Bounds的加强版,支持可以对类型进行隐式转换,将指定的类型进行隐式转换后,再判断是否在边界指定的类型范围内
  3. 案例:跟小狗交朋友
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 = Student@5a22eec0

scala> val doggy = new Dog("doggy")
doggy: Dog = Dog@231a08d

scala> val party = new Party(leo,doggy)
party: Party[Object] = Party@7e0cbe79

Context Bounds

  1. Context Bounds是一种特殊的Bounds,它会根据泛型类型的声明,比如“T:类型”要求必须存在一个类型为“类型[T]”的隐式值。其实个人认为,Context Bounds之所以叫做Context,是因为它基于的是一种全局的上下文,需要使用到上下文中的隐式值以及注入
  2. 案例:使用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] = Calculator@723c7f2f

scala> ca.max
res8: Int = 23

Manifest Context Bounds

  1. 在scala中,如果要实例化一个泛型数组,就必须使用Manifest Context Bounds,也就是说,如果数组元素类型为T的话,需要为类或者函数定义[T:Manifest]泛型类型,这样才能实例化Array[T]这种泛型数组
  2. 案例:打包饭菜(一种食品打成一包)
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 = Meat@330a0dbb

scala> val shoushibaocai = new Meat("shoushibaocai")
shoushibaocai: Meat = Meat@6e74986c

scala> val meatPackage = packageFood(gongbaojiding,shoushibaocai)
meatPackage: Array[Meat] = Array(Meat@330a0dbb, Meat@6e74986c)

协变和逆变

  1. scala的协变和逆变完全解决了java中的泛型的一大缺憾
  2. 举例来说,java中,如果有professional是Master的子类,那么Card(Professionnal)是不是Card(Master)的子类呢?答案是:不是。
  3. 而scala中,只要灵活使用协变和逆变,就可以解决java泛型的问题
  4. 案例:进入会场
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] = Card@762637be

scala> val jack = new Card[Professional]("jack")
jack: Card[Professional] = Card@4aa11206

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] = Card@3700994c

scala> val jack = new Card[Professional]("jack")
jack: Card[Professional] = Card@62581ef6

scala> enterMeet(leo)
welcome to have this meeting!

scala> enterMeet(jack)
welcome to have this meeting!

Existential Type

  1. 在scala中,有一种特殊的类型参数,就是Existential Type存在性类型。
Array[T] forSome {type T}
//占位符
Array[_]

Spark基础-scala学习(七、类型参数)

标签:sorry   不能   extends   import   灵活   泛型数组   define   length   多个   

原文地址:https://www.cnblogs.com/sky-chen/p/10128927.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!