package study;
class Person{
public void fun1() {
System.out.println("父类的fun1方法");
}
public void fun2() {
System.out.println("*父类的fun2方法");
this.fun1();
}
}
class Child extends Person{
public void fun1() {
System.out.println("子类的fun1方法");
}
public void fun3() {
System.out.println("*子类的fun3方法");
this.fun1();
}
}
public class exercise {
public static void main(String[] args) {
Child a=new Child();//实例子类对象,向下转之前必有向上转
Child b=(Child)a;//向下转自动转换,现上则需手动转换
b.fun1();//此方法被子类覆写过,使用子类的fun1的方法
a.fun2();
//无法通过a调用fun3方法,
}
}
14.1多态向下转
最新推荐文章于 2024-03-29 22:31:21 发布