Scala学习:类2

object test15 {
//类的继承
abstract class Person(name: String,age: Int){
println("constructing Person")
def walk(): Unit ={
println(name +" person walk")
}
}
class Student(name:String,age:Int,studentNo:String) extends Person(name,age){
println("constructing Student")

//方法重写
override def walk(): Unit = {
println(name+" student walk")
}
}



def main(args: Array[String]): Unit = {
val student=new Student("jack",25,"no.128")
student.walk()

//匿名类
//当某个类在程序中只使用一次时,可以将类定义为匿名类
val s=new Person("Paul",15){
override def walk(): Unit = {
println("匿名walk")
}
}
s.walk()




}


}
posted @ 2019-05-27 20:12  等木鱼的猫  阅读(135)  评论(0编辑  收藏  举报