Scala interface-oriented programming and Spark source code appreciation

1. Scala implements the interface with the keyword extends

2. If there is the extends keyword before, use the keyword with to implement the interface

3. Scala's abstract properties. The properties that are not instantiated are abstract. You must specify the type of the abstract property val name: Sring, which can be assigned directly in the subclass




4. The method in the trait is inherited and compared with the inheritance of the ordinary class: it is not the same, the trait inherited is directly added to the subclass.


5. The instance object of the Scala class can also be mixed into the interface to extend the function of the instance of the current object

 

trait Logger {
  def log(message : String) :Unit = {
    println("Logger "+message)
  }
}
trait RichLogger extends Logger{
 override def log(message : String) :Unit = {
   println("RichLogger " + message)
 }
}
class loggin (val name : String ) extends Logger {
  def loggin: Unit = {
    println("Hi ,welcome "+name)
    log(name)
  }
}
object HelloTrait {
  def  main(args : Array[String]) :Unit = {
    val personInfo = new loggin("DTSpark") with RichLogger
    personInfo.loggin
  }
}


6. When there are multiple traits, load is executed sequentially from right to left

Guess you like

Origin blog.csdn.net/superiorpengFight/article/details/54406571