Overload(重载) Override(覆写)Overwrite(重写)

时间:2022-08-23 09:18:31
C++:
Overload(重载):函数或者方法有同样的名称,但是参数列表不相同的情形,这样的同名不同参数的函数或者方法之间,互相称之为重载函数或者方法。
(1)相同的范围(同一个类)
(2)函数名相同
(3)函数参数不同
Override(覆写):从字面就可以知道,它是覆盖了一个方法并且对其重写,以求达到不同的作用。对我们来说最熟悉的覆盖就是对接口方法的实现,在接口中一般只是对方法进行了声明,而我们在实现时,就需要实现接口声明的所有方法。除了这个典型的用法以外,我们在继承中也可能会在子类覆盖父类中的方法。
(1)不同的范围(分别位于派生类与基类);
(2)函数名字相同;
(3)参数相同;
(4)基类函数必须有virtual 关键字。

Overwrite(重写):指派生类的函数屏蔽了与其同名的基类函数

(1)如果派生类的函数与基类的函数同名,但是参数不同。此时,不论有无virtual关键字,基类的函数将被隐藏(注意别与重载混淆)。

(2)如果派生类的函数与基类的函数同名,并且参数也相同,但是基类函数没有virtual关键字。此时,基类的函数被隐藏(注意别与覆盖混淆)。

#include <iostream>

using namespace std;

class TestOverload
{
public:
TestOverload(){}
virtual ~TestOverload(){}

// 重载Overload
void MyOverload(int a);
void MyOverload(int a, int b);
virtual void MyOverload(int a, int b, int c);
};

void TestOverload::MyOverload(int a)
{
cout << "MyOverload a-" << "a:" << a << endl;
}

void TestOverload::MyOverload(int a, int b)
{
cout << "MyOverload a b--" << "a:" << a << endl;
cout << "MyOverload a b--" << "b:" << b << endl;
}

void TestOverload::MyOverload(int a, int b, int c)
{
cout << "MyOverload a b c--" << "a:" << a << endl;
cout << "MyOverload a b c--" << "b:" << b << endl;
cout << "MyOverload a b c--" << "c:" << c << endl;
}

class TestOverride : public TestOverload
{
public:
TestOverride(){};
virtual ~TestOverride(){}

// 重写Overwrite
void MyOverload(int a);
// 覆写Override
virtual void MyOverload(int a, int b, int c);
};

void TestOverride::MyOverload(int a)
{
cout << "MyOverwrite a-" << "a:" << a << endl;
}

void TestOverride::MyOverload(int a, int b, int c)
{
cout << "MyOverride a b c--" << "a:" << a << endl;
cout << "MyOverride a b c--" << "b:" << b << endl;
cout << "MyOverride a b c--" << "c:" << c << endl;
}

int main()
{
TestOverload t;
t.MyOverload(1);
t.MyOverload(2, 3);
t.MyOverload(5, 6, 7);

cout << "--------------" << endl;

TestOverload* pt = new TestOverride();
pt->MyOverload(8);
((TestOverride*)pt)->MyOverload(18);
pt->MyOverload(9, 10, 11);
}



C#:
overload (重载)关键字: 无
override (覆写)关键字: override  
overwrite (重写)关键字:new

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AceCSharpConsole
{
class TestOverload
{
public TestOverload(){}
~TestOverload(){}

// 重载Overload
public void MyOverload(int a)
{
Console.WriteLine("MyOverload a:{0}", a);
}

public void MyOverload(int a, int b)
{
Console.WriteLine("MyOverload a:{0}", a);
Console.WriteLine("MyOverload b:{0}", b);
}

public virtual void MyOverload(int a, int b, int c)
{
Console.WriteLine("MyOverload a:{0}", a);
Console.WriteLine("MyOverload b:{0}", b);
Console.WriteLine("MyOverload c:{0}", c);
}
};

class TestOverride : TestOverload
{
public TestOverride(){}
~TestOverride(){}

// 重写Overwrite
new public void MyOverload(int a)
{
Console.WriteLine("MyOverwrite {0}", a);
}

// 覆写Override
override public void MyOverload(int a, int b, int c)
{
Console.WriteLine("MyOverride a:{0}", a);
Console.WriteLine("MyOverride b:{0}", b);
Console.WriteLine("MyOverride c:{0}", c);
}
};

class Program
{
static void Main(string[] args)
{
TestOverload t = new TestOverload();
t.MyOverload(1);
t.MyOverload(2, 3);
t.MyOverload(5, 6, 7);

TestOverload tp = new TestOverride();
tp.MyOverload(8);
((TestOverride)tp).MyOverload(18);
tp.MyOverload(9, 10, 11);

}
}
}