zoukankan      html  css  js  c++  java
  • JAVA & C++ 多态

      多态,也叫动态绑定。

    Java:

    class A
    {
        public void f1()
        {
            System.out.println("A:f1");
        }
        public void f2()
        {
            System.out.println("A:f2");
        }
    };
    class B extends A
    {
        public void f1()
        {
            System.out.println("B:f1");
        }
        public void f3()
        {
            System.out.println("B:f3");
        }
    };
    public class Helloworld {
        public static void main(String args[]) {
                B b = new B();
                A a = b;
                b.f1();        //B:f1
                a.f1();        //B:f1
        }
    }

      此处所示的是Java中的向上转型,如果所调用的方法被子类覆写过,则调用的是子类中被覆写之后的方法。

      Java形成多态(特指运行时多态)的条件:1. 子类中覆写了父类中的某个/某些方法    2. 父类引用指向子类对象(向上转型)

      两个条件同时具备时,才会发生多态,缺一不可。

      多态实际上是一种机制,在编译时刻,会生成一张虚拟表,来记录所有覆盖的方法,没有被覆盖的方法是不会记录到这张表的。

    若一个父类引用调用了没有覆盖的子类方法,那么是不符合该表的,那么编译时刻就会报错。 在执行程序的时候,虚拟机会去这张虚

    拟表中找覆盖的方法,比如当父类引用中存的是一个子类对象时,就会去找子类中的相应的覆盖的方法来执行。

    C++:

    #include <iostream>
    using namespace std;
    
    class A
    {
    public:
        void f1()
        {
            cout << "A:f1" << endl;
        }
         void f2()
        {
            cout << "A:f2" << endl;
        }
    };
    class B : public A
    {
    public:
        void f1()
        {
            cout << "B:f1" << endl;
        }
        void f3()
        {
            cout << "B:f3" << endl;
        }
    };
    int main() {
        B b;
        A a = b;
        b.f1();        //B:f1
        a.f1();        //A:f1
        return 0;
    }

      此处所示的是C++中的向上转型,此处即使所调用的方法被子类覆写,但并没有形成多态,则调用的仍是父类中的方法。

      C++形成多态的条件: 1. C++父类中必须要有虚函数且子类重写该虚函数    2. 必须存在父类的指针或引用指向子类对象。

      两个条件同时具备时,才会发生多态,缺一不可。

      对于这段代码,如果想实现多态,可以采用以下的代码:

    #include <iostream>
    using namespace std;
    
    class A
    {
    public:
        virtual void f1()    //the keyword 'virtual' is essential here.
        {
            cout << "A:f1" << endl;
        }
         void f2()
        {
            cout << "A:f2" << endl;
        }
    };
    class B : public A
    {
    public:
        void f1()    //the keyword 'virtual' is not essential here.
        {
            cout << "B:f1" << endl;
        }
        void f3()
        {
            cout << "B:f3" << endl;
        }
    };
    int main() {
        B b;
        //1:   A a = b;
        //2:   A * a = &b;
        A &a = b;
    
        b.f1();              //B:f1
    
        //1:   a.f1();       //A:f1
        //2:   (*a).f1();    //B:f1
        a.f1();              //B:f1
        return 0;
    }

      通过上面的对比,我们能够看出,C++和Java中的多态本质上是一样的,只不过在实现多态时,存在一些语言特性上的细微差别。

    参考文章:

    java多态的含义:http://edu.zyitrc.com/html/201304/88.html

    
    
  • 相关阅读:
    hdu 2485 Destroying the bus stations 迭代加深搜索
    hdu 2487 Ugly Windows 模拟
    hdu 2492 Ping pong 线段树
    hdu 1059 Dividing 多重背包
    hdu 3315 My Brute 费用流,费用最小且代价最小
    第四天 下载网络图片显示
    第三天 单元测试和数据库操作
    第二天 布局文件
    第一天 安卓简介
    Android 获取存储空间
  • 原文地址:https://www.cnblogs.com/lxw0109/p/JAVA_CPP.html
Copyright © 2011-2022 走看看