上QQ阅读APP看书,第一时间看更新
Same signatures and return types
Consider an example where we want to mix two traits into a class and their declaration of a method is identical:
trait FormalGreeting {
def hello(): String
}
trait InformalGreeting {
def hello(): String
}
class Greeter extends FormalGreeting with InformalGreeting {
override def hello(): String = "Good morning, sir/madam!"
}
object GreeterUser {
def main(args: Array[String]): Unit = {
val greeter = new Greeter()
System.out.println(greeter.hello())
}
}
In the preceding example, the greeter is always polite and mixes both formal and informal greetings. While implementing, it just has to implement the method once.