标签:sorry 不能 extends import 灵活 泛型数组 define length 多个
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
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
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
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.
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
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
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)
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!
Array[T] forSome {type T}
//占位符
Array[_]
标签:sorry 不能 extends import 灵活 泛型数组 define length 多个
原文地址:https://www.cnblogs.com/sky-chen/p/10128927.html