Scala2.11.7 Study Notes (vi) Introduction to Scala traits

Source: Internet
Author: User
Tags traits

Characteristics

Scala's trait can be implemented in a way that compares to Java's interface. Scala's trait supports the blending of classes and singleton objects with multiple trait (using methods from these trait, without violating the principle of single inheritance).

Scala defines an app trait type for the Singleton object's main, so the above example can be simplified to:

Helloworld.scalaobject HelloWorld extends App {println ("Hello world!");}

This code cannot be run as a script, and Scala's script requires that the code end with an expression. So to run this code, you need to compile the code before you run it.

Run directly for the first time without any output G:\hadoop\scala-sdk\source>scala helloworld.scala//to compile G:\hadoop\scala-sdk\source>scalac helloworld.scala//run G:\hadoop\scala-sdk\source>scala Helloworldhello world! again

Note: Scala provides a quick-compile code for the Auxiliary command FSC (Fast Scala Compliler), which uses this command to start the JVM only the first time the FSC is used, and then the FSC runs in the background, thus avoiding the need to load the associated library file every time the Scalac is used. Thus increasing the speed of compilation.


Multiple inheritance


Trait use


Trait structure


Specialty Applications



This article is from the "Ruchunli's work Notes" blog, so be sure to keep this source http://luchunli.blog.51cto.com/2368057/1705025

Scala2.11.7 Study Notes (vi) Introduction to Scala traits

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.