本文共 1879 字,大约阅读时间需要 6 分钟。
目录
本篇做了一些非常有趣的验证,在不同语言版本之间,探索一下函数重载,在类继承中的行为表现。
如果子类中的函数名称与基类中的某个成员函数相同,则子类对象能否调用这个基类的函数?
#includeusing namespace std;class Base{public: int f(int i) { cout << "f(int): "; return i+3; }};class Derived : public Base{public: double f(double d) { cout << "f(double): "; return d+3.3; }};int main(){ Derived* dp = new Derived; cout << dp->f(3) << '\n'; cout << dp->f(3.3) << '\n'; delete dp; return 0;}
输出结果:
f(double): 6.3
f(double): 6.6 而不是预期的结果:
f(int) : 6 f(double): 6.6
结论:C++中,重载对于子类是无效的。
因为基类与子类之间没有重载的关系。对于这个程序,编译器只会在子类范围中查找,会找出函数“double f(double)”并调用它,绝对不会查找到基类的范围中。 C++中没有跨范围的重载,这个例子并不是重载规则中的某种特殊情况。同时,我们试一下Java版本的重载看看。 just for funny coding :-)
class Base{ public int f(int i) { System.out.print("f (int): "); return i+3; }}class Derived extends Base{ public double f(double i) { System.out.print("f (double) : "); return i + 3.3; }}class myprogram3{ public static void main(String args[]) { Derived obj = new Derived(); System.out.println(obj.f(3)); System.out.println(obj.f(3.3)); }}
输出结果:
f (int): 6 f (double): 6.6结论:Java的重载跨越了类的继承范围。Java编译器在编译期间,调用了对应的重载函数,这是基于参数类型实现的。
那就也试试C#版本的吧。 :-)
using System; class Base{ public int f(int i) { Console.Write("f (int): "); return i + 3; }}class Derived : Base{ public double f(double i) { Console.Write("f (double) : "); return i+3.3; }}class MyProgram{ static void Main(string[] args) { Derived obj = new Derived(); Console.WriteLine(obj.f(3)); Console.WriteLine(obj.f(3.3)); Console.ReadKey(); // visual studio使用,用于halt console,类似C/C++的getch. }}
输出结果:
f(double) : 6.3 f(double) : 6.6结论:C#与C++类似,在基类与子类之间没有重载功能。在C#中,没有跨类范围的重载,所以它并不是重载规则的一个特殊情况。
转载地址:http://rmeji.baihongyu.com/