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个黑眼圈熊猫大侠讲中文
时间: 2024-10-12 05:31:23