依旧父与子

22 阅读1分钟
object class03 {
  class Fruit() {
    def eat(): Unit = {
      println("eat...")
    }
  }

  class Apple extends Fruit {
    override def eat(): Unit = {
      println("吃薄果皮,中间的不能吃")
    }
  }

  class Watermelon extends Fruit {
    override def eat(): Unit = {
      println("削皮,中间的最好吃")
    }
  }

  def main(args: Array[String]): Unit = {
    // 参数类型:父类
    def test(fruit: Fruit): Unit = {
      fruit.eat()
    }

    val a1 = new Apple()
    test(a1) // 传入子类

    val w1 = new Watermelon()
    test(w1)
  }
}
object class04 {
  class Father() {
    println("Father 的构造器....")
  }

  class Son extends Father() {
    println("Son 的构造器....")
  }

  def main(args: Array[String]): Unit = {
    new Son()
  }
}