码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-07-03 16:13:18      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:scala   smart   animal   ima   父类   array   and   unit   package   

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

标签:scala   smart   animal   ima   父类   array   and   unit   package   

原文地址:http://www.cnblogs.com/IChing/p/7111142.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!