多态

多态

package oop.demo7;

public class Demo7 {
  public static void main(String[] args) {
      //一个对象的实际类型是确定的 new Student()

      Student student=new Student();
      //Student能调用的方法:自己和父类的

      Person student2=new Student();
      //可以指向子类,但不能调用子类独有的方法

      student.show();
      //子类
      student.show2();
      //子类
      student2.show();
      //小秘密
  }
}

class Person{
  void show(){
      System.out.println("父类");
  }


}
class Student extends Person{

  @Override
  void show() {
      System.out.println("子类");
  }
  void show2(){
      System.out.println("小秘密");
  }
}

 

posted @ 2021-10-08 17:30  clown-lan  阅读(8)  评论(0)    收藏  举报