The override and super scala

override和super
Similar to the Java language, we need to use the override in a subclass to override the parent class member, you can use super to refer to the parent class
Usage
subclasses to override a method in the parent class, you must use the override keyword
to use the override to override a val field
using the super keyword to access a member of the parent class
example
example shows
the definition of a Person class that contains
the name field (not re-assignment)
to obtain the name of the method
to define a Student class
rewrite the name field
override the acquisition method name, return "hello," + name
to create a Student object example, calls its getName method
reference code

class Person {
  val name = "super"
 def getName = name
}
class Student extends Person {
  // 重写val字段
  override val name: String = "child"
  // 重写getName方法
  override def getName: String = "hello, " + super.getName
}
object Main13 {
  def main(args: Array[String]): Unit = {
    println(new Student().getName)
  }
}

Here Insert Picture Description
answer:

Here Insert Picture Description

Published 139 original articles · won praise 333 · views 210 000 +

Guess you like

Origin blog.csdn.net/qq_45765882/article/details/104302803