首页 > 代码库 > scala 基础九 scala 类的扩展和继承extends override

scala 基础九 scala 类的扩展和继承extends override

1.scala 类的继承

package smart.iot//父类 animal 有三个参数class animal(var name:String,var age:Int,var eat:String) {  }//子类 panda 继承父类 animal class Panda( name:String, age:Int, eat:String,var blackeye:String) extends animal(name,age,eat){    }object load{  def main(args: Array[String]): Unit = {      var panda=new Panda("panpan",2,"竹子","2个黑眼圈");      println("panda "+"Name"+panda.name+"age:"+panda.age+"eat:"+panda.eat+"blackeye:"+panda.blackeye)  }   }

 2.方法的重写和参数的重写 override

package smart.iot//父类 animal 有三个参数class animal(var name:String,var age:Int,var eat:String) {      val wing:String="啊啊啊";   def talk()=println("say english");}//子类 panda 继承父类 animal class Panda( name:String, age:Int, eat:String,var blackeye:String) extends animal(name,age,eat){    override val wing:String="熊猫大侠";    override def talk()=println("讲中文");}object load{  def main(args: Array[String]): Unit = {      var panda=new Panda("panpan",2,"竹子","2个黑眼圈");      println("panda "+"Name"+panda.name+"age:"+panda.age+"eat:"+panda.eat+"blackeye:"+panda.blackeye)      println(panda.wing)      panda.talk()  }   }

resout:
panda Namepanpanage:2eat:竹子blackeye:2个黑眼圈
熊猫大侠
讲中文

 

scala 基础九 scala 类的扩展和继承extends override