scala 基础九 scala 类的扩展和继承extends override
Posted jialiming
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章