The deferred binding of the trait method is called after the trait method is first mixed. This can be seen from the example in the previous section.
Let's look at a similar example below:
Abstract classWriter {def write (message:string): string}trait upperwriterextendsWriter {AbstractOverride Def write (message:string): String =Super. Write (message.touppercase)}trait filterwriterextendsWriter {AbstractOverride Def write (message:string): String =Super. Write (Message.replace (' O ', '-'))}trait StringWriterextendsWriter {def write (message:string): String=Message}val MyWriter1=NewStringWriter with Upperwriter with Filterwriterval MyWriter2=NewStringWriter with Filterwriter with Upperwriterprintln (MyWriter1 write"Hello world!") println (MyWriter2 write"Hello world!")
An abstract class and three trait are defined in the code.
The abstract class writer only defines an abstract method, and does not provide a concrete implementation. Therefore, the trait that inherits the abstract class writer must implement the Write method.
Upperwriter's Write method implements the conversion of the incoming English character to uppercase;
Filterwriter's Write method implements the substitution of lowercase "o" with "-";
StringWriter simply returns the passed-in string as it is.
Take a look at the results of the above code:
Validates our argument that delayed binding is the first trait to be executed after a mix-up.
The order of execution of MyWriter1:filterwriter–> upperwriter–> StringWriter;
The execution order of MyWriter2:upperwriter–> filterwriter–> StringWriter.
Deferred binding from trait it is easy to think of the initialization order of the parent class and subclass of Java. Or is the responsibility chain pattern in Java. So it's not hard to think of Java to do this: you can take a different sequence of responsibility chains, or you can use different inheritance sequences to implement them.
Furthermore, there is little difference between the trait and abstract classes in Scala from these two sections:
- Can have common methods and abstract methods;
- can have ordinary member variables and abstract variables;
- Abstract classes can do things that trait can do.
So where are the differences between them:
- Trait can be mixed in multiple, abstract class can only inherit;
- Abstract classes can define constructors;
- Trait can be mixed with instances, abstract classes are not available.
When to use trait, when to use abstract classes:
- Prefer to use trait. It is convenient to extend multiple trait for a class, but only one abstract class can be extended.
- If you need constructor arguments, use an abstract class. Because an abstract class can define a constructor with parameters, trait does not. For example, you cannot say trait T (i:int) {}, parameter I is illegal.
Reference Documentation:
Https://twitter.github.io/scala_school/zh_cn/basics.html
Http://stackoverflow.com/questions/1991042/what-is-the-advantage-of-using-abstract-classes-instead-of-traits
http://www.artima.com/pins1ed/traits.html#12.7
##############
Scala Learning notes delayed binding of 34-trait methods