傳送門(mén)
Scala入門(mén)系列之一--使用命令行往文件輸入數(shù)據(jù)并讀取數(shù)據(jù)
Scala入門(mén)系列之二--數(shù)組、元組谁不、列表、集合數(shù)據(jù)類(lèi)型簡(jiǎn)述
Scala入門(mén)系列之三--類(lèi)和方法的創(chuàng)建以及命令行執(zhí)行scala文件
Scala入門(mén)系列之四--類(lèi)成員的可見(jiàn)性(private)以及用value和value_=進(jìn)行讀取和修改
Scala入門(mén)系列之五--主構(gòu)造器和輔助構(gòu)造器
Scala入門(mén)系列之六--伴生對(duì)象和伴生類(lèi)
Scala入門(mén)系列之七--scala中的繼承
Scala入門(mén)系列之八--scala中的特質(zhì)(trait)徽诲,也稱接口
Scala入門(mén)系列之九--模式匹配兩大法寶(match語(yǔ)句和case類(lèi))
Scala入門(mén)系列之十--包
Scala入門(mén)系列之十一--函數(shù)式編程基礎(chǔ)
Scala入門(mén)系列之十二--高階函數(shù)
Scala入門(mén)系列之十三--針對(duì)容器的操作(遍歷/映射/過(guò)濾/規(guī)約操作)
Scala入門(mén)系列之十四--使用IDEA編寫(xiě)Scala代碼并Maven打包提交集群運(yùn)行
傳送門(mén)
一刹帕、特質(zhì)(trait),也稱接口
1)基本概念
- scala中沒(méi)有接口的概念谎替,而是提供了特質(zhì)(trait)
- scala的特質(zhì)是代碼重用的基本單元偷溺,可以同時(shí)擁有抽象方法和具體方法
- 特質(zhì)(trait)可以被多重繼承,從而重用特質(zhì)中的方法和字段钱贯。
- 特質(zhì)可以使用extends繼承其它的特質(zhì)
//特質(zhì)的定義
trait Flyable{
val maxFlyHeight:Int //抽象字段
def fly()
def breathe(){println("I can!")} //具體方法
}
//與抽象類(lèi)的區(qū)別
//同:特質(zhì)既可以包含抽象成員挫掏,也可以包含非抽象成員。
//異:包含抽象成員時(shí)秩命,不需要abstract關(guān)鍵字尉共。(就是說(shuō)直接用trait定義就好)
2)Scala中特質(zhì)trait與抽象類(lèi)abstract的區(qū)別
- 優(yōu)先使用特質(zhì)。一個(gè)類(lèi)擴(kuò)展多個(gè)特質(zhì)很方便的硫麻,但只能擴(kuò)展一個(gè)抽象類(lèi)
- 如果你需要構(gòu)造函數(shù)參數(shù)爸邢,使用抽象類(lèi)。因?yàn)槌橄箢?lèi)是可以定義帶參數(shù)的構(gòu)造函數(shù)拿愧,而特質(zhì)比較麻煩杠河。例如:你不能trait t(i:int){},參數(shù)i是非法的
//特質(zhì)要實(shí)現(xiàn)帶參數(shù)浇辜,可以在方法里面去定義
trait helloworld{
def sayhello(name:String)
}
3)把特質(zhì)混入類(lèi)中
- 可以使用extends或with關(guān)鍵字調(diào)用特質(zhì)方法
- 如果特質(zhì)中包含抽象成員券敌,則該類(lèi)必須為這些抽象成員提供具體實(shí)現(xiàn),除非該類(lèi)被定義為抽象類(lèi)(即上一節(jié)中的抽象類(lèi))
//混入單個(gè)特質(zhì)trait
trait Flyable{
var maxFlyHeight:Int //抽象字段
def fly() //抽象方法
def breathe(){println("I can breathe")} //具體的方法
}
class Bird(flyHeight:Int) extends Flyable{
var maxFlyHeight:Int = flyHeight //重載特質(zhì)的抽象字段
def fly(){
println("I can fly at the height of" + maxFlyHeight) //重載特質(zhì)的抽象方法
}
}
object Bird{
def main(args:Array[String]){
val v = new Bird(100)
v.fly() //I can fly at the height of 100
v.breathe() //I can breathe
}
}
//混入多個(gè)特質(zhì)
trait Flyable{
var maxFlyHeight:Int //抽象字段
def fly() //抽象方法
def breathe(){println("I can breathe")} //具體的方法
}
trait HasLegs{
var legs:Int
def move(){println("I can walk with "+ legs + "legs")}
}
class Animal(val category:String){
def info(){println("This is a "+category)}
}
class Bird(flyHeight:Int) extends Animal("Bird") with Flyable with HssLegs{
var maxFlyHeight:Int = flyHeight //重載特質(zhì)的抽象字段
var legs = 2 //重載特質(zhì)的抽象字段
def fly(){
println("I can fly at the height of" + maxFlyHeight) //重載特質(zhì)的抽象方法
}
}
object Bird{
def main(args:Array[String]){
val v = new Bird(100)
v.fly() //I can fly at the height of 100
v.breathe() //I can breathe
b.info() //This is a Bird
}
}
4)練習(xí):
定義一個(gè)父類(lèi)person柳洋,里面包含人的類(lèi)別字段和一個(gè)方法待诅,人的類(lèi)別字段當(dāng)作構(gòu)造方法參數(shù)傳入,方法返回人的類(lèi)別
定義一個(gè)特質(zhì)醫(yī)生熊镣,里面包含一個(gè)醫(yī)生行為方法卑雁,看材际椤(抽象方法)
定義一個(gè)特質(zhì)教師,里面包含一個(gè)教師行為方法测蹲,上課(抽象方法)
再寫(xiě)一個(gè)子類(lèi)莹捡,里面繼承person類(lèi),根據(jù)實(shí)際類(lèi)別扣甲,混入1個(gè)特質(zhì)篮赢,
子類(lèi)創(chuàng)建后能夠調(diào)用父類(lèi)和特質(zhì)的方法
//定義一個(gè)父類(lèi)person,里面包含人的類(lèi)別字段和一個(gè)方法琉挖,人的類(lèi)別字段當(dāng)作構(gòu)造方法參數(shù)傳入启泣,方法返回人的類(lèi)別
//定義一個(gè)特質(zhì)醫(yī)生,里面包含一個(gè)醫(yī)生行為方法示辈,看擦让!(抽象方法)
//定義一個(gè)特質(zhì)教師,里面包含一個(gè)教師行為方法矾麻,上課(抽象方法)
//再寫(xiě)一個(gè)子類(lèi)坠敷,里面繼承person類(lèi),根據(jù)實(shí)際類(lèi)別射富,混入1個(gè)特質(zhì)膝迎,
//子類(lèi)創(chuàng)建后能夠調(diào)用父類(lèi)和特質(zhì)的方法
class person(var people:String){
def info(){println("hello, I am " + people)}
}
trait doctor{
def working()
}
trait teacher{
def classing()
}
class son extends person("teacher") with teacher{
def teacher(){
println("He is a teacher")
}
}
object myScala{
def main(args:Array[String]){
var s = new son()
s.teacher() //He is a teacher
s.info() //hello, I am teacher
}
}