/*
* 定义抽象类和java一样,使用abstract关键字
* 抽象类中的成员(变量、方法)不需要初始化或占位符站位,也不需要加abstract
*/
abstract class SuperTeacher(val name: String) {
var id: Int
var age: Int
def teach
}
/*
* 和java一样,实现抽象类,使用extends关键字
*/
class TeacherForMaths(name: String) extends SuperTeacher(name) {
override var id = name.hashCode()
override var age = 29
override def teach {
println("Teaching...")
}
}
object Test {
def main(args: Array[String]): Unit = {
val teacher = new TeacherForMaths("tom")
teacher.teach
println("teacher.id: " + teacher.id)
println(teacher.name + " : " + teacher.age)
}
}
scala基础15-抽象(类、字段、方法)
最新推荐文章于 2023-04-24 10:31:21 发布