Java对Override方法调用采取的是运行时绑定,也就是按照对象的实际类型来决定调用的方法,而不是按照对象的声明类型来决定调用方法,是一种向上转型。而Overload方法则想法是在编译时进行静态绑定的,按照声明类型决定调用的方法。这点C++也是一样的class father
{
void f()
{
System.out.println("father::f");
}
}
public class Son extends father{
void f()
{
System.out.println("son::f");
}
public static void main(String[] argv)
{
father f = new Son();
f.f();
}
}运行结果:son::fC++支持两种多态性:a编译时多态性:通过重载函数实现,b运行时多态性:通过虚函数实现。最常见的用法就是声明基类的指针,利用该指针指向任意一个子类对象,调用相应的虚函数,可以根据指向的子类的不同而实现不同的方法。如果没有使用虚函数的话,即没有利用C++多态性,则利用基类指针调用相应的函数的时候,将总被限制在基类函数本身,而无法调用到子类中被重写过的函数。#include <iostream>
using namespace std;
class B{
public:
virtual void f() const {cout<<"B::f";}
void k() {cout<<"B::k";}
};class D : public B
{
public:
void f() const {cout<<"D::f";}//overrides
void k() const{cout<<"D::k";}
};main(void)
{
B *ptr,b;
D d;
d.k();
ptr =new D;
ptr->f();
ptr->k();
b.f();
b.k();
}运行结果:D::kD::fB::kB::fB::k《
C++ 设计新思维》 下载见 http://www.linuxidc.com/Linux/2014-07/104850.htm
C++ Primer Plus 第6版 中文版 清晰有书签PDF+源代码 http://www.linuxidc.com/Linux/2014-05/101227.htm读C++ Primer 之构造函数陷阱 http://www.linuxidc.com/Linux/2011-08/40176.htm读C++ Primer 之智能指针 http://www.linuxidc.com/Linux/2011-08/40177.htm读C++ Primer 之句柄类 http://www.linuxidc.com/Linux/2011-08/40175.htm
将C语言梳理一下,分布在以下10个章节中:- Linux-C成长之路(一):Linux下C编程概要 http://www.linuxidc.com/Linux/2014-05/101242.htm
- Linux-C成长之路(二):基本数据类型 http://www.linuxidc.com/Linux/2014-05/101242p2.htm
- Linux-C成长之路(三):基本IO函数操作 http://www.linuxidc.com/Linux/2014-05/101242p3.htm
- Linux-C成长之路(四):运算符 http://www.linuxidc.com/Linux/2014-05/101242p4.htm
- Linux-C成长之路(五):控制流 http://www.linuxidc.com/Linux/2014-05/101242p5.htm
- Linux-C成长之路(六):函数要义 http://www.linuxidc.com/Linux/2014-05/101242p6.htm
- Linux-C成长之路(七):数组与指针 http://www.linuxidc.com/Linux/2014-05/101242p7.htm
- Linux-C成长之路(八):存储类,动态内存 http://www.linuxidc.com/Linux/2014-05/101242p8.htm
- Linux-C成长之路(九):复合数据类型 http://www.linuxidc.com/Linux/2014-05/101242p9.htm
- Linux-C成长之路(十):其他高级议题
本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-10/108584.htm