不是特别懂,但感觉和RUBY当中的MIX-IN功能有几分相似,这又扯到了多重继承及JAVA当中的接口虚拟类了。。
package com.hengheng.scala class UseTrait { } trait Logger { def log(msg : String) { println("log : " + msg) } } trait ConsoleLogger extends Logger { override def log(msg : String) { println(msg) } } class Test extends ConsoleLogger { def test() { log("Here is Spark!") } } class ConcreteLogger extends Logger { def concreteLog { log("It‘s me!") } } object UseTrait extends App { var logger = new ConcreteLogger logger.concreteLog var test = new Test test.test }
输出:
log : It‘s me!
Here is Spark!
时间: 2024-10-16 18:08:43