本篇內(nèi)容主要講解“Scala trait的對象怎么使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“Scala trait的對象怎么使用”吧!
創(chuàng)新互聯(lián)公司:公司2013年成立為各行業(yè)開拓出企業(yè)自己的“網(wǎng)站建設(shè)”服務(wù),為千余家公司企業(yè)提供了專業(yè)的成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計和網(wǎng)站推廣服務(wù), 定制網(wǎng)站由設(shè)計師親自精心設(shè)計,設(shè)計的效果完全按照客戶的要求,并適當(dāng)?shù)奶岢龊侠淼慕ㄗh,擁有的視覺效果,策劃師分析客戶的同行競爭對手,根據(jù)客戶的實際情況給出合理的網(wǎng)站構(gòu)架,制作客戶同行業(yè)具有領(lǐng)先地位的。
一、trait
trait Logger {
def log(msg: String)
}
class ConsoleLogger extends Logger with Serializable{
def log(msg: String) {println(msg)}
}
注意,沒有implements,用extends就好了,另外,這里其實也是override,但是不需要寫上。log在Logger當(dāng)中是抽象的,如果要實現(xiàn)多個trait,用with連上就行了。另外,跟java一樣,一個父類+若干個Interface
二、有具體實現(xiàn)的特質(zhì)
trait Logger {
def log(msg: String) {println(msg)}
}
class Account(val name: String, val password: String) extends Logger {
def check() = {log("Account = " + name + " and password = " + password); name == "bajie" && password == "change" }
}
object Account {
def main(args: Array[String]) {
val acc = new Account("bajie", "change")
acc.check
}
}
前一節(jié)當(dāng)中,對trait的使用,類似于java的interface。那么本節(jié)當(dāng)中,它該怎么理解?
三、帶有trait的對象
trait Logger {
def log(msg: String) {}
}
trait ConsoleLogger extends Logger {
override def log(msg: String) {println(msg)}
}
class Account(val name: String, val password: String) extends Logger {
def check() = {log("Account = " + name + " and password = " + password); name == "bajie" && password == "change" }
}
object Account {
def main(args: Array[String]) {
val acc = new Account("bajie", "change")
acc.check
val acc1 = new Account("bajie", "change") with ConsoleLogger
acc1.check
}
}
又做了一些修改, Logger.log有一個空實現(xiàn), ConsoleLogger.log干了活了, Account 混合了 Logger ,所以 acc.check ,并不做輸出的工作,而 val acc1 = new Account("bajie", "change") with ConsoleLogger ,acc1.check有輸出了。
好吧,目前我還想不清楚這樣的一種設(shè)計,具體應(yīng)該用來完成什么樣的工作,但是它一定是有用的,盡量記住它吧。
四、特質(zhì)的疊加(屌爆了)
trait Logger {
def getLog: String = "I am the root; "
}
trait ConsoleLogger extends Logger {
override def getLog = super.getLog + "Something happend "
}
trait TimeLogger extends Logger{
override def getLog = super.getLog + "at " + new Date() + " "
}
class Account(val name: String, val password: String) extends Logger {
def check() = {println(getLog); name == "bajie" && password == "change" }
}
object Account {
def main(args: Array[String]) {
val acc1 = new Account("bajie", "change") with TimeLogger with ConsoleLogger
acc1.check
val acc2 = new Account("bajie", "change") with ConsoleLogger with TimeLogger
acc2.check
}
}
最后是輸出
I am the root;
I am the root; at Fri Aug 12 01:32:24 PDT 2016 Something happend
I am the root; Something happend at Fri Aug 12 01:32:24 PDT 2016
體會一下吧,說不清楚了!
五、interface + abstract class
trait Logger {
def log(message: String)
def info(message: String) {println(message)}
}
class Account extends Logger {
var accName = "bajie"
var accPass = "change"
def check() = {info("a user login "); accName == "bajie" && accPass == "change"}
def log(message: String) { println(message) }
}
在這里L(fēng)ogger,既是一個interface,又是一個abstract class。Account可以直接使用info方法,又必須自己實現(xiàn)log方法。
六、具體屬性和抽象屬性
trait Logger {
def log(message: String)
def info(message: String) {println(message)}
val logLevel: Int
val maxlength = 20
}
class Account extends Logger {
var accName = "bajie"
var accPass = "change"
def check() = {info("a user login "); accName == "bajie" && accPass == "change"}
def log(message: String) { println(message) }
val logLevel = 3
}
和前面一節(jié)類似,屬性也有具體和抽象之分,具體字段可以再Account當(dāng)中直接使用,抽象字段必須具體化。
到此,相信大家對“Scala trait的對象怎么使用”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!