标签:inter 使用 $1 define ati 支持 知识 ring rri
scala> :paste
// Entering paste mode (ctrl-D to finish)
trait HelloTrait{
def sayHello(name:String)
}
trait MakeFriendsTrait{
def makeFriend(p:Person)
}
class Person(val name:String) extends HelloTrait with MakeFriendsTrait with Cloneable with Serializable
{
def sayHello(name:String) = println("Hello," + name)
def makeFriend(p:Person) = println("Hello,my name is "+name +",your name is "+p.name)
}
// Exiting paste mode, now interpreting.
defined trait HelloTrait
defined trait MakeFriendsTrait
defined class Person
scala> val p = new Person("Tom")
p: Person = Person@41eb94bc
scala> p.makeFriend(p)
Hello,my name is Tom,your name is Tom
scala> p.sayHello("jike")
Hello,jike
scala> :paste
// Entering paste mode (ctrl-D to finish)
trait Logger{
def log(message :String) = println(message)
}
class Person(val name:String) extends Logger{
def makeFriends(p:Person){
println("Hi,I‘m "+name+",I‘m glad to make friends with you," + p.name)
log("makeFriends method is invoked with parameter Person[name="+p.name+"]")
}
}
// Exiting paste mode, now interpreting.
defined trait Logger
defined class Person
scala> val p = new Person("Tom")
p: Person = Person@6a6e9289
scala> p.makeFriends(p)
Hi,I‘m Tom,I‘m glad to make friends with you,Tom
makeFriends method is invoked with parameter Person[name=Tom]
scala> :paste
// Entering paste mode (ctrl-D to finish)
trait Person{
val eyeNum:Int = 2
}
class Student(val name:String) extends Person{
def sayHello = println("Hi,I‘m "+name+",I have "+eyeNum+" eyes")
}
// Exiting paste mode, now interpreting.
defined trait Person
defined class Student
scala> val s = new Student("Tom")
s: Student = Student@12765ebd
scala> s.sayHello
Hi,I‘m Tom,I have 2 eyes
scala> :paste
// Entering paste mode (ctrl-D to finish)
trait SayHello{
val msg:String
def sayHello(name:String) = println(msg+","+name)
}
class Person(val name:String) extends SayHello{
val msg:String = "hello"
def makeFriends(p:Person){
sayHello(p.name)
print("I‘m "+name+",I want to make friends with you!")
}
}
// Exiting paste mode, now interpreting.
defined trait SayHello
defined class Person
scala> val p = new Person("Tom")
p: Person = Person@67cd193d
scala> p.msg
res4: String = hello
scala> p.makeFriends(p)
hello,Tom
I‘m Tom,I want to make friends with you!
scala> :paste
// Entering paste mode (ctrl-D to finish)
trait Logged{
def log(msg:String){}
}
trait MyLogger extends Logged{
override def log(msg:String){println("log:"+msg)}
}
class Person(val name:String) extends Logged{
def sayHello{println("Hi,I‘m "+name);log("sayHello is invoked!")}
}
// Exiting paste mode, now interpreting.
defined trait Logged
defined trait MyLogger
defined class Person
scala> val p1 = new Person("leo")
p1: Person = Person@20307cb9
scala> p1.sayHello
Hi,I‘m leo
scala> val p2 = new Person("jack") with MyLogger
p2: Person with MyLogger = $anon$1@192800d
scala> p2.sayHello
Hi,I‘m jack
log:sayHello is invoked!
scala> :paste
// Entering paste mode (ctrl-D to finish)
trait Handler{
def handle(data:String){}
}
trait DataValidHandler extends Handler{
override def handle(data:String){
println("check data:" +data)
super.handle(data)
}
}
trait SignatureValidHandler extends Handler {
override def handle(data:String){
println("check signature: "+data)
super.handle(data)
}
}
class Person(val name:String) extends SignatureValidHandler with DataValidHandler{
def sayHello = { println("Hello, " + name);handle(name)}
}
// Exiting paste mode, now interpreting.
defined trait Handler
defined trait DataValidHandler
defined trait SignatureValidHandler
defined class Person
scala> val p1 = new Person("Tom")
p1: Person = Person@5fefb35c
scala> p1.sayHello
Hello, Tom
check data:Tom
check signature: Tom
trait Logger{
def log(msg:String)
}
trait MyLogger extends Logger{
abstract override def log(msg:String) {super.log(msg)}
}
scala> :paste
// Entering paste mode (ctrl-D to finish)
trait Valid{
def getName:String
def valid:Boolean = {
getName == "leo"
}
}
class Person(val name:String) extends Valid{
println(valid)
def getName = name
}
// Exiting paste mode, now interpreting.
defined trait Valid
defined class Person
scala> val p = new Person("leo")
true
p: Person = Person@30749de8
scala> p.getName
res10: String = leo
scala> val p2 = new Person("Tom")
false
p2: Person = Person@622a4589
scala> p2.getName
res11: String = Tom
scala> :paste
// Entering paste mode (ctrl-D to finish)
class Person{println("Person constructor")}
trait Logger{println("Logger constructor")}
trait MyLogger extends Logger{println("MyLogger constructor")}
trait TimeLogger extends Logger{println("TimeLogger constructor")}
class Student extends Person with MyLogger with TimeLogger{
println("Student constructor")
}
// Exiting paste mode, now interpreting.
defined class Person
defined trait Logger
defined trait MyLogger
defined trait TimeLogger
defined class Student
scala> val s = new Student
Person constructor
Logger constructor
MyLogger constructor
TimeLogger constructor
Student constructor
s: Student = Student@22caeb7f
scala> :paste
// Entering paste mode (ctrl-D to finish)
trait SayHello{
val msg:String
println(msg:String)
}
class Person
val p = new {
val msg:String = "init"
} with Person with SayHello
// Exiting paste mode, now interpreting.
init
defined trait SayHello
defined class Person
p: Person with SayHello = $anon$1@6ce7fb0c
scala> :paste
// Entering paste mode (ctrl-D to finish)
class Person extends{
val msg:String = "init"
} with SayHello{}
// Exiting paste mode, now interpreting.
defined class Person
scala> val p2 = new Person
init
p2: Person = Person@16c87aa2
scala> :paste
// Entering paste mode (ctrl-D to finish)
trait SayHello{
lazy val msg:String = null
println(msg.toString)
}
class Person extends SayHello{
override lazy val msg:String = "init"
}
// Exiting paste mode, now interpreting.
defined trait SayHello
defined class Person
scala> val p3 = new Person
init
p3: Person = Person@3219b550
scala> :paste
// Entering paste mode (ctrl-D to finish)
class MyUtil{
def printMessage(msg:String) = println(msg)
}
trait Logger extends MyUtil{
def log(msg:String) = printMessage("log: "+msg)
}
class Person(val name:String) extends Logger{
def sayHello{
log("Hi,I‘m "+name)
printMessage("Hi,I‘m "+name)
}
}
// Exiting paste mode, now interpreting.
defined class MyUtil
defined trait Logger
defined class Person
scala> val p = new Person("Jike")
p: Person = Person@3685cd73
scala> p.sayHello
log: Hi,I‘m Jike
Hi,I‘m Jike
标签:inter 使用 $1 define ati 支持 知识 ring rri
原文地址:https://www.cnblogs.com/sky-chen/p/10099716.html