Scala trait的对象怎么使用
本篇内容主要讲解"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的对象怎么使用"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!