Scala Learning notes delayed binding of 34-trait methods

Source: Internet
Author: User
Tags traits

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:

    1. Can have common methods and abstract methods;
    2. can have ordinary member variables and abstract variables;
    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.