您好,登錄后才能下訂單哦!
本篇內容主要講解“Scala trait的對象怎么使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Scala trait的對象怎么使用”吧!
一、trait
trait Logger {
def log(msg: String)
}
class ConsoleLogger extends Logger with Serializable{
def log(msg: String) {println(msg)}
}
注意,沒有implements,用extends就好了,另外,這里其實也是override,但是不需要寫上。log在Logger當中是抽象的,如果要實現多個trait,用with連上就行了。另外,跟java一樣,一個父類+若干個Interface
二、有具體實現的特質
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
}
}
前一節當中,對trait的使用,類似于java的interface。那么本節當中,它該怎么理解?
三、帶有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有一個空實現, ConsoleLogger.log干了活了, Account 混合了 Logger ,所以 acc.check ,并不做輸出的工作,而 val acc1 = new Account("bajie", "change") with ConsoleLogger ,acc1.check有輸出了。
好吧,目前我還想不清楚這樣的一種設計,具體應該用來完成什么樣的工作,但是它一定是有用的,盡量記住它吧。
四、特質的疊加(屌爆了)
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) }
}
在這里Logger,既是一個interface,又是一個abstract class。Account可以直接使用info方法,又必須自己實現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
}
和前面一節類似,屬性也有具體和抽象之分,具體字段可以再Account當中直接使用,抽象字段必須具體化。
到此,相信大家對“Scala trait的對象怎么使用”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。