Big Data series Cultivation-scala Course 05

Source: Internet
Author: User

    • Scala multi-inheritance, constructor execution order, AOP implementation
      • Trait implementations of multiple inheritance: Scala interfaces can inherit specific classes, trait interfaces can implement multiple inheritance, and a class can inherit a particular class, which can be mixed in after inheritance, the implementation of an interface
      • Multi-inheritance constructor execution order: The execution order of the multi-inheritance constructor is performed sequentially from left to right, and if the preceding class and inherited implementations are implemented, the following classes do not need to be implemented, and the parent class simply executes the
Object Triatscala {def main (args:array[string]): Unit={val T1=Newpianoteacher T1.playpiano T1.teach}}//Public parent Classclasshuman{println ("Human")}//inherit the interface of the parent classTrait TteacherextendsHuman {println ("Tteacher") def teach}//inherits the interface of the parent class and has its own methodTrait PianoplayerextendsHuman {println ("Pianoplayer") def Playpiano= {println ("I ' m playing piano."))} }  //inherits the class of the parent class and mixes in two interfaces implements the method implementation of the interface in the order that is left to rightclassPianoteacherextendsHuman with Tteacher with Pianoplayer {override def teach= {println ("I ' m training students."))} }
    • AOP code implementation based on trait: the interface is inherited by another interface and also invokes the parent class abstract method when implementing the parent class method, in another class is the method of implementing the common interface, so that AOP can be implemented (aspect-oriented programming)
    • //AOP-oriented tangent programmingtrait Action {def doAction}trait tbeforeafterextendsAction {//using an abstract method to override the parent class's interface implementation requires a method of making the parent class, which is implemented in a specific class  Abstractoverride def doAction {println ("Initialization")     Super. DoAction println ("Destroyed")   }  }classWorkextendsaction{//Implement the parent class method specificallyOverride Def doAction = println ("Working ...")}object Triatscala {def main (args:array[string]): Unit={val work=NewWork with Tbeforeafter work.doaction}}
  • package Definition and implicit reference implementation of package object, package reference, package
    • Package definition: The definition of a package in Scala is that you can create multiple packages under a common package, you need an instance object of a class in a package, you can new the object under the package, and the package
    • Package object implementations: The methods and properties inside the package object that can be accessed in other packages
    • The reference implementation of the package: referencing the related class in a package with an import followed by {} including the referenced object, you can also use the Java package = = to swap aliases, and _ represents all the following packages
    •  Package Com.scala.spark    Package object People {    = "Scala"  }  package  people {  class  people {    = DefaultName}  import  java.awt.{ Color,font}import java.util. {hashmap=>Javahashmap} Import Scala. {StringBuilder = _}

    • Baoyin: There are several implicit packages in Scala, such as the Java.lang._,scala._,predef_ three packages, which are like several common packages in Java and have many tool methods.
  • about access permissions for packages, classes, objects, members, associated classes, and associated objects in Scala
    • Access rights for packages, classes, objects, Members: private[package name] means that the method, package, class, object, member access rights are extended to the package below
    • Associated class, associated object access: associated objects and associated classes can access each other
    •  Packagespark{ Packagenavigation{//Private[spark] Indicates that the class has access rights under the spark package    Private[Spark]classnavigator{//Protected[navigation] Indicates that the method has access rights under the navigation package      protected[navigation] def usestarchart () {}classLegofjourney {//Private[navigator] Indicates that the constant has access rights under the navigation package        Private[Navigator] Val distance =100      }      //Private[this] Indicates that the constant has access rights under the navigation package      Private[ This] var speed = 200    }  }   Packagelaunch{ImportNavigation._ Object Vehicle {//Private[launch] Indicates that the constant is instantiated in the package launch the sibling      Private[Launch] Val guide =NewNavigator} }}

  • Read and write files in Scala, console input operations
    • Read-write implementation of the file: Read the file with the FromFile method in the source object to read the text data can also be used fromurl follow the URL to read, and write with PrintWriter to achieve
    • Console operation implementation: Console.ReadLine to implement console operation, console this in Scala's implicit package, you can directly use

Learning here today .....

Baidu Video Address: http://pan.baidu.com/s/1ntzOmDz

Big Data series cultivation-scala course

Related Article

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.