abstract class SuperTeacher(val name : String){
var id : Int //抽象字段,不需要初始化
var age : Int
def teach //抽象方法
}
class TeacherForMaths(name : String) extends SuperTeacher(name){
override var id = name.hashCode()
override var age = 29
override def teach{
println("Teaching!!!")
}
}
object AbstractClassOps{
def main(args: Array[String]) {
val teacher = new TeacherForMaths("Spark")
teacher.teach
println("teacher.id" + ":" + teacher.id)
println(teacher.name + ":" + teacher.age)
}
}
结果:
Teaching!!!
teacher.id:80085693
Spark:29
网友评论