1. 说明

    Java一样,可以通过包含带有定义或重写的代码块的方式创建一个匿名的子类

    2.案例实操

    1. abstract class Person {
    2. val name: String
    3. def hello(): Unit
    4. }
    5. object Test {
    6. def main(args: Array[String]): Unit = {
    7. //抽象类不能被实例化,可以通过匿名子类的方式创建对象
    8. val person = new Person {
    9. override val name: String = "teacher"
    10. override def hello(): Unit = println("hello teacher")
    11. }
    12. }
    13. }