scala入门初级代码训练-10类的继承
原创文章,转载请注明出处!
原文地址: http://www.ptbird.cn/2016/07/19/scala-chuji-demo-10/
There I am,in the world more exciting!
by postbird
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
}
}
文章版权:Postbird-There I am , in the world more exciting!
本文链接:http://ptbird.cn/scala-chuji-demo-10.html
转载请注明文章原始出处 !