1、Trait基礎設計模式
一、將trait做爲接口使用ide
// Scala中的Triat是一種特殊的概念
// 首先咱們能夠將Trait做爲接口來使用,此時的Triat就與Java中的接口很是相似
// 在triat中能夠定義抽象方法,就與抽象類中的抽象方法同樣,只要不給出方法的具體實現便可
// 類可使用extends關鍵字繼承trait,注意,這裏不是implement,而是extends,在scala中沒有implement的概念,不管繼承類仍是trait,統一都是extends
// 類繼承trait後,必須實現其中的抽象方法,實現時不須要使用override關鍵字
// scala不支持對類進行多繼承,可是支持多重繼承trait,使用with關鍵字便可
###
scala> :paste
// Entering paste mode (ctrl-D to finish)
trait SayHello {
def sayHello(name: String)
}
trait MakeFriends {
def makeFriends(p: Person)
}
class Person(val name: String) extends SayHello with MakeFriends {
def sayHello(otherName: String) = println("Hello, " + otherName + ", I'm " + name)
def makeFriends(p: Person) = println("Hello " + p.name + ", I'm " + name + ", I want to make friends with you.")
}
// Exiting paste mode, now interpreting.
defined trait SayHello
defined trait MakeFriends
defined class Person
scala> val p = new Person("leo")
p: Person = Person@223f3642
scala> val p2 = new Person("jack")
p2: Person = Person@48fa0f47
scala> p.sayHello("jack")
Hello, jack, I'm leo
scala> p.makeFriends(p2)
Hello jack, I'm leo, I want to make friends with you.
二、在Trait中定義具體方法函數
// Scala中的Triat能夠不是隻定義抽象方法,還能夠定義具體方法,此時trait更像是包含了通用工具方法的東西// 有一個專有的名詞來形容這種狀況,
就是說trait的功能混入了類
// 舉例來講,trait中能夠包含一些不少類都通用的功能方法,好比打印日誌等等,spark中就使用了trait來定義了通用的日誌打印方法
###
scala> :paste
// Entering paste mode (ctrl-D to finish)
trait Logger {
def log(msg: String) = println("log: " + msg)
}
class Person(val name: String) extends Logger {
def sayHello {
println("Hello, I'm " + name); log("sayHello is invoked")
}
}
// Exiting paste mode, now interpreting.
defined trait Logger
defined class Person
scala> val p = new Person("leo")
p: Person = Person@2898ac89
scala> p.sayHello
Hello, I'm leo
log: sayHello is invoked
三、在Trait中定義具體字段工具
// Scala中的Triat能夠定義具體field,此時繼承trait的類就自動得到了trait中定義的field
// 可是這種獲取field的方式與繼承class是不一樣的:若是是繼承class獲取的field,實際是定義在父類中的;而繼承trait獲取的field,就直接被添加到了類中
###
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("leo")
s: Student = Student@4b0d79fc
scala> s.sayHello
Hi, I'm leo, I have 2eyes.
四、在Trait中定義抽象字段spa
// Scala中的Triat能夠定義抽象field,而trait中的具體方法則能夠基於抽象field來編寫
// 可是繼承trait的類,則必須覆蓋抽象field,提供具體的值
###
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)
println("I;m " + name + ", I want to make friend with you.")
}
}
// Exiting paste mode, now interpreting.
defined trait SayHello
defined class Person
scala> val p1 = new Person("leo")
p1: Person = Person@62656be4
scala> val p2 = new Person("jack")
p2: Person = Person@56dc1551
scala> p1.makeFriends(p2)
hello, jack
I;m leo, I want to make friend with you.
2、trait高級scala
一、爲實例混入trait設計
// 有時咱們能夠在建立類的對象時,指定該對象混入某個trait,這樣,就只有這個對象混入該trait的方法,而類的其餘對象則沒有
###
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@35d08e6c
scala> p1.sayHello
Hi, I'm leo
scala> val p2 = new Person("jack") with MyLogger
p2: Person with MyLogger = $anon$1@1bb564e2
scala> p2.sayHello
Hi, I'm jack
log: sayHello is invoked!
二、trait調用鏈日誌
// Scala中支持讓類繼承多個trait後,依次調用多個trait中的同一個方法,只要讓多個trait的同一個方法中,在最後都執行super.方法便可
// 類中調用多個trait中都有的這個方法時,首先會從最右邊的trait的方法開始執行,而後依次往左執行,造成一個調用鏈條
// 這種特性很是強大,其實就至關於設計模式中的責任鏈模式的一種具體實現
###
scala> :paste
// Entering paste mode (ctrl-D to finish)
trait Handler {
def handle(data: String) {}
}
trait DataVailHandler extends Handler {
override def handle(data: String) {
println("check data: " + data)
super.handle(data)
}
}
trait SignatureVaildHandler extends Handler {
override def handle(data: String) {
println("check signature: " + data)
}
}
class Person(val name: String) extends SignatureVaildHandler with DataVailHandler {
def sayHello = {println("Hello," + name); handle(name)}
}
// Exiting paste mode, now interpreting.
defined trait Handler
defined trait DataVailHandler
defined trait SignatureVaildHandler
defined class Person
scala> val p = new Person("leo")
p: Person = Person@545de5a4
scala> p.sayHello
Hello,leo
check data: leo
check signature: leo
三、在trait中覆蓋抽象方法code
// 在trait中,是能夠覆蓋父trait的抽象方法的
// 可是覆蓋時,若是使用了super.方法的代碼,則沒法經過編譯。由於super.方法就會去掉用父trait的抽象方法,此時子trait的該方法仍是會被認爲是抽象的
// 此時若是要經過編譯,就得給子trait的方法加上abstract override修飾
###
scala> :paste
// Entering paste mode (ctrl-D to finish)
trait Logger {
def log(msg: String)
}
trait Mylogger extends Logger {
abstract override def log(msg: String) {super.log(msg)}
}
// Exiting paste mode, now interpreting.
defined trait Logger
defined trait Mylogger
四、混合使用trait的具體方法和抽象方法對象
// 在trait中,能夠混合使用具體方法和抽象方法
// 可讓具體方法依賴於抽象方法,而抽象方法則放到繼承trait的類中去實現
// 這種trait其實就是設計模式中的模板設計模式的體現
###
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@3f093abe
五、trait的構造機制
// 在Scala中,trait也是有構造代碼的,也就是trait中的,不包含在任何方法中的代碼
// 而繼承了trait的類的構造機制以下:一、父類的構造函數執行;二、trait的構造代碼執行,多個trait從左到右依次執行;
三、構造trait時會先構造父trait,若是多個trait繼承同一個父trait,則父trait只會構造一次;四、全部trait構造完畢以後,子類的構造函數執行
###
scala> :paste
// Entering paste mode (ctrl-D to finish)
class Person {
println("Persons's constructor!")
}
trait Logger {
println("Logger's constructor!")
}
trait MyLogger extends Logger {
println("MyLogger's constructor!")
}
trait TimeLogger extends Logger {
println("TimeLogger's constructor!")
}
class Student extends Person with MyLogger with TimeLogger {
println("Student's 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
Persons's constructor!
Logger's constructor!
MyLogger's constructor!
TimeLogger's constructor!
Student's constructor!
s: Student = Student@158d255c
六、trait field的初始化
// 在Scala中,trait是沒有接收參數的構造函數的,這是trait與class的惟一區別,可是若是需求就是要trait可以對field進行初始化,該怎麼辦呢?
只能使用Scala中很是特殊的一種高級特性 - 提早定義
###
scala> trait sayHello {
| val msg: String
| println(msg.toString)
| }
defined trait sayHello
scala> class Person
defined class Person
scala> val p = new {
| val msg: String = "init"
| } with Person with sayHello
init
p: Person with sayHello = $anon$1@1af7f54a
七、trait繼承class
// 在Scala中,trait也能夠繼承自class,此時這個class就會成爲全部繼承該trait的類的父類
###
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("leo")
p: Person = Person@6650813a
scala> p.sayHello
log: Hi, I'm leo
Hi, I'mleo