class Person3 {
 public void fun1() {
  System.out.println("1.Person1()");
 }
 public void fun2() {
  System.out.println("2.Person2()");
 }
}
class Student extends Person3 {
 public void fun1() {
  System.out.println("3.Person3()");
 }
 public void fun4() {
  System.out.println("4.Person4()");
 }
}
public class Test14 {
 public static void main(String[]args) {
  Person3 p = new Student();//类的多态之“向上转型”:父类对象通过子类对象去实例化
  Student s = (Student)p; //“向下转型”:父类对象可以转化为子类对象
  s.fun1();                //向上转型自动完成,向下转型必须进行强制类型转换
 }
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-25
  • 2021-05-20
  • 2023-03-17
  • 2022-12-23
  • 2022-02-16
  • 2022-12-23
猜你喜欢
  • 2021-10-31
  • 2021-08-11
  • 2022-01-14
  • 2021-07-16
  • 2022-12-23
  • 2022-12-23
  • 2021-11-27
相关资源
相似解决方案