首页 > 代码库 > 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。