scala入门初级代码训练-12trait基础

原创文章,转载请注明出处!

原文地址: http://www.ptbird.cn/2016/07/19/scala-chuji-demo-12/

There I am,in the world more exciting!

by postbird

www.ptbird.cn


12trait基础


 

package com.ptbird.scala

/**
* Created by postbird on 2016/5/25.
* trait 比 interface 强大很多
*/
/**
* trait 也可以有具体的实现方法
*/
trait Logger {
def log(msg: String): Unit ={
println("Logger log")
}
}

class ConcreteLogger extends Logger {
override def log(msg: String) = {
println("log: " + msg)
}

def concreteLog: Unit = {
log("IT's me!!")
}
}

object TraitTest {
def main(args: Array[String]): Unit = {
val logger=new ConcreteLogger
logger.concreteLog
}
}

欢迎评论。
lingkb » scala入门初级代码训练-12trait基础

发表评论