原创文章,转载请注明出处!

原文地址: http://www.ptbird.cn/2016/07/19/scala-chuji-demo-10/

There I am,in the world more exciting!

by postbird

www.ptbird.cn


10类的继承


 
package com.ptbird.scala /** * Created by postbird on 2016/5/25. */ class Person1(val name: String, var age: Int) { println("The primary constructer of Person") val school = "DHU" 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 is the subClass of Person,Primary constructor of Worker") override val school = "aaa" //重写school成员 override def toString = "I am a Worker!" + super.sleep //重写toString // super 在子类中继承父类的相关成员 } object ClassTestExtends { def main(args: Array[String]): Unit = { val w=new Worker("Prbirtd",18,10000) println(w.school) println(w.toString) println(w.salary) // The primary constructer of Person // This is the subClass of Person,Primary constructor of Worker // aaa // I am a Worker!8 hours // 10000 } }