zoukankan      html  css  js  c++  java
  • 多态中的题目分析题

    多态(Polymorphic)概述:事物存在的多种形态

    多态的前提:a.要有继承关系 b.要有方法重写 c.要有父类引用指向子类对象

    多态中的成员访问特点之成员变量:编译看左边(父类),运行看左边(父类)

    多态中的成员访问特点之成员方法:编译看左边(父类),运行看右边(子类) 动态性

    分析多态中的题目:

    =======A程序============

    class Fu {
    public void show() {
    System.out.println("fu show");
    }
    }

    class Zi extends Fu {
    public void show() {
    System.out.println("zi show");
    }

    public void method() {
    System.out.println("zi method");
    }
    }

    class Test1Demo {
    public static void main(String[] args) {
    Fu f = new Zi();
    f.method();
    f.show();
    }
    }

    由于Fu中没有method()方法,由此该程序在编译的时候就出现问题

    =======B程序============


    class A {
    public void show() {
    show2();
    }
    public void show2() {
    System.out.println("我");
    }
    }
    class B extends A {
    public void show2() {
    System.out.println("爱");
    }
    }
    class C extends B {
    public void show() {
    super.show();
    }
    public void show2() {
    System.out.println("你");
    }
    }
    public class Test2DuoTai {
    public static void main(String[] args) {
    A a = new B();
    a.show();

    B b = new C();
    b.show();
    }
    }

    程序的结果:

    爱 你

    分析:

    A a = new B();

    a.show();   编译看B的父类A有show()方法,编译通过 运行看右边 子类没有show()方法 但是子类继承了父类的方法 用父类A 的show()方法,show()方法 调研show2()(子类对show2()重写)结果为 爱

    B b = new C();
    b.show();   编译看左边 父类B由于继承了A 所有有show()方法,编译通过 运行看右边子类show()方法 里面调用super.show2()调用子类的show2() 结果为 你

    转载于:https://www.cnblogs.com/taowangwang/p/9475954.html

  • 相关阅读:
    HTML <form> 标签的 accept-charset 属性
    SpringMVC中Controller跳转到另一个Controller方法
    DIV层+CSS实现锁屏
    工作组模式下专用队列(Private Queue)如何引用远程队列路径
    stl之deque双端队列容器
    百度之星资格赛——Disk Schedule(双调旅行商问题)
    android 无线模式下使用ADB调试
    HDU
    NoSQL 数据库产品学习总结(一)
    ThinkPad E431怎样关闭触摸板
  • 原文地址:https://www.cnblogs.com/twodog/p/12136201.html
Copyright © 2011-2022 走看看