代码如下: package test41 //泛型特质 object test3 { //定义一个日志//泛型特质,X是泛型名称,可以更改。trait Logger[X] {val content: Xdef show():Unit }class FileLogger extends Logger[String] {override val content: String = "file"override def show(): Unit = {println(content)}}def main(args: Array[String]): Unit = {val fileLogger = new FileLogger()fileLogger.show()} }
运行结果如图: