Scala's inheritance object-oriented programming

extends keyword

 

// Scala , so that subclasses inherit the parent class, and Java , as is the use extends keyword

// inherited represents, subclasses inherit from the parent class the parent class field and Method ; subclass can then placed in a parent class does not have its own internal, subclass specific field and Method ; effective use of code reuse inheritance

// subclass can override the parent class field and method ; however, if the parent with final modifications, field and method with a final modification, the class can not be inherited, field and method are not covered

 

class Person {

  private var name = "leo"

  def getName = name

}

class Student extends Person {

  private var score = "A"

  def getScore = score

}

 

override and super

// Scala , the subclasses to cover a non-abstract method in the parent class, you must use the override keyword

// override keywords can help us find code errors as early as possible, such as: the override method name modified parent class method we misspelled; parameters such as the parent class method to override us wrong; etc.

// In addition, after the subclass override the parent class method, if we are in a subclass method is to call the parent class is covered by it? It can use the super keyword, explicitly specify the method to call the parent class

 

class Person {

  private var name = "leo"

  def getName = name

}

class Student extends Person {

  private var score = "A"

  def getScore = score

  override def getName = "Hi, I'm " + super.getName

}

 

override field

// Scala , the subclass can override the parent class val field , and subclasses val field may also cover the parent class val field of getter methods; just use in a subclass override keyword to

 

class Person {

  val name: String = "Person"

  def age: Int = 0

}

 

class Student extends Person {

  override val name: String = "leo"

  override val age: Int = 30

}

 

isInstanceOfasInstanceOf

 

// If we create a subclass of the object, but in turn gives the parent class type variable. In the follow-up program, we need to convert the parent class is a subclass of the type of a variable type of variable, what should we do?

// First, use isInstanceOf determines whether the specified object is an object class, and if so, may be used asInstanceOf convert the specified object type

// Note that if the object is null , then isInstanceOf must return false , , asInstanceOf, must return null

// Note that if you did not use isInstanceOf first determine whether the object is an instance of the specified class, the direct use of asInstanceOf conversion, you may throw an exception

 

class Person

class Student extends Person

val p: Person =  new Student

was s Student = zero

if (p.isInstanceOf[Student]) s = p.asInstanceOf[Student]

 

getClassclassOf

// isInstanceOf if it is determined that only the specified object is an object class and its subclasses, but can not be accurately judged, is the object that the specified class

// If the target is required to accurately determine the object of the specified class, then it can only use getClass and classOf the

// objects .getClass can be accurately acquired object class, classOf [ class ] can accurately obtain class, then == operator can determine

 

class Person

class Student extends Person

val p: Person = new Student

p.isInstanceOf[Person]

p.getClass == classOf[Person]

p.getClass == classOf[Student]

 

 

Using pattern matching type judgment

// However, in actual development, such as spark source, a large number of places are used to perform pattern matching manner to determine the type, which is much more concise manner, and the code to obtain scalability and maintainability also very high

// use pattern matching, for the functional, and isInstanceOf like is determined mainly target class and subclass of this class can not accurately judge

 

class Person

class Student extends Person

val p: Person = new Student

 

p match {

  case per: Person => println("it's Person's object")

  case _  => println("unknown type")

}

 

protected

// with java as, Scala in use can also be protected to modify the keywords field and method , so that subclasses do not need to super keyword, you can directly access the field and method

// You can also use protected [the this] , you can only access the parent class in the current sub-class object field and method , can not be accessed by other subclass object parent class field and method

 

class Person {

  protected var name: String = "leo"

  protected[this] var hobby: String = "game"

}

class Student extends Person {

  def sayHello = println("Hello, " + name)

  def makeFriends(s: Student) {

    println("my hobby is " + hobby + ", your hobby is " + s.hobby)

  }

}

Call the parent class's constructor

// Scala , each class can have a primary constructor and any plurality of auxiliary constructor , and each auxiliary constructor of the first row must be called by other auxiliary constructor or the primary constructor ; thus helper class constructor is certainly not You may directly call the parent class constructor of

// only primary subclass constructor call the parent class's constructor , this syntax is to call the constructor of the parent class through the main constructor of the subclass

// Note! If the parent class is received parameters, such as name and Age , when a subclass to receive, do not use any val or var to modify, and otherwise it will be considered a subclass overrides the parent class field

class Person(val name: String, val age: Int)

class Student(name: String, age: Int, var score: Double) extends Person(name, age) {

  def this(name: String) {

    this(name, 0, 0)

  }

  def this(age: Int) {

    this("leo", age, 0)

  }

}

 

 

Anonymous inner classes

// In Scala , the anonymous subclass is very common, and very powerful. Spark source code is also extensive use of this anonymous subclasses.

// anonymous subclasses, that is to say, you can not define a subclass of class name, and directly create its object and object reference to a variable. After the object can even pass an anonymous subclass to other functions.

 

class Person(protected val name: String) {

  def sayHello = "Hello, I'm " + name

}

val p = new Person("leo") {

  override def sayHello = "Hi, I'm " + name

}

def greeting(p: Person { def sayHello: String }) {

  println(p.sayHello)

}

 

Abstract class

// If the parent class, there are certain methods can not be achieved immediately, but need to rely on different child to cover, rewritten to achieve their own different ways. At this time, the parent class may be these methods do not give a specific implementation, only the method signature, this method is an abstract method.

// And if a class has an abstract method, then type must be used abstract be declared as an abstract class, an abstract class is not possible at this time instantiated

// When the abstract method of the abstract class overridden in subclasses need not be used override keyword

 

abstract class Person(val name: String) {

  def sayHello: Unit

}

class Student(name: String) extends Person(name) {

  def sayHello: Unit = println("Hello, " + name)

}

 

 

Abstract field

// If the parent class is defined field , but did not give an initial value, this field abstract field

// abstract field means, Scala will be according to their own rules, as var or val type of field to generate the corresponding getter and setter methods, but the parent class is not the field of

// subclass must override Field, , to define their own specific Field, , and covers abstract Field, , no need to use override keyword

 

abstract class Person {

  val name: String

}

 

class Student extends Person {

  val name: String = "leo"

}

Guess you like

Origin www.cnblogs.com/YuanWeiBlogger/p/11432419.html