The scala trait inherited class

trait inherited class
definitions
The class can also be inherited trait. Qualities will be a member of the class are inherited.
Examples
example illustrates
定义一个特质,继承自一个class
the steps
to create a MyUtils class that defines printMsg method
to create a Logger trait, inherited from MyUtils, defined log methods
create a Person class, add the name field
inherit Logger qualities
to achieve sayHello method, call the log method
to add the main method, create a Person object call sayHello method
reference code:

class MyUtil {
    def printMsg(msg:String) = println(msg)
}

trait Logger extends MyUtil {
    def log(msg:String) = printMsg("Logger:" + msg)
}

class Person extends Logger {
    def sayHello() = log("你好")
}

def main(args: Array[String]): Unit = {
    val person = new Person
    person.sayHello()
}

Here Insert Picture Description
result:
Here Insert Picture Description

Published 139 original articles · won praise 333 · views 210 000 +

Guess you like

Origin blog.csdn.net/qq_45765882/article/details/104303791