scala教程:super关键字

说明

子类的方法,可以调用父类的方法,通过 super 关键字实现

案例

class Phone(var cpu:String) {

  def call(num:String):Unit = {
    println("用 "+cpu+" 给 "+num+" 打电话")
  }
}

class IPhone(cpu:String) extends Phone(cpu) {

  def call110():Unit = {
    super.call("110") // 通过super关键字调用父类的call()方法
  }
}

object Phone{
  def main(args: Array[String]): Unit = {

    val phone:IPhone = new IPhone("A100")
    phone.call110() 

  }
}

原文出处:/show_1IX1DEdLgJzB.html