DT大数据梦工厂 温故而知新 之12讲

王家林亲授 大数据开发语言《Scala深入浅出实战经典》(1-71讲全部视频、代码、PPT)下载 ,总共有140讲Scala视频!
百度云: http://pan.baidu.com/s/1jGjFpWy
腾讯微云: http://url.cn/TnGbdC

360云盘:http://yunpan.cn/cQ4c2UALDjSKy 访问密码 45e2


DT大数据梦工厂 温故而知新 之12讲scala 中的继承

package TestPractice


/**
 * @author Administrator
 */
object Person {
  
   def  main(args:Array[String]){
     val w =new Worker("Spark",5,10000)
     println("w.school="+w.school)
     println("w.salary="+w.salary)
     println("w.sleep="+w.sleep)
     println("w.toString="+w.toString)
   }
  
  
}


class Person1(val name:String , var age:Int){
  println("this primary  constructor of Person")
  
  val school ="BUG"
  def sleep="8 hours"
  override def toString="I am  a person1"
}
//构造参数不能写定义修饰符
class Worker(name:String ,age:Int ,val salary:Long) extends Person1(name , age){
   println("this primary  constructor of worker")
   override val school="sss"  
   override def toString="I am  a work "+ super.sleep

run 结果:

this primary  constructor of Person
this primary  constructor of worker
w.school=sss
w.salary=10000
w.sleep=8 hours
w.toString=I am  a work 8 hours


总结 :子类继承父类的时候,必须要把父类的构造器跳虫 并且不能定义其修饰符 ,即 在子类声明的时候调用父类分构造参数


你可能感兴趣的:(scala)