thinkinjava--7.7向上转型

1.子类拥有基类得所有方法

2.子类本身可以看作是一个基类(向上转型),子类可能拥有比基类更多得方法,在向上转型得过程中可能丢失。

代码说明:

基类:
public class Aanimal {
    void jump(){System.out.println("父类跳");};
    void eat(){};
    void sleep(){};
    void change(Aanimal a){};
}
子类:
public class Frog extends Aanimal {
    public static void main(String args[]) {
        Frog frog = new Frog();
        frog.eat();
        frog.sleep();//可以调用基类得方法
        frog.change(frog);//frog向上转型成animal
        frog.jump();
    }

    @Override
    void jump() {
        // super.jump();
        System.out.println("儿子跳");
    }
}

重写方法调用子类重写后的方法。

猜你喜欢

转载自blog.csdn.net/stonennnn/article/details/80803514