超载,超载

重载和重写之间的区别是什么。

178783 次浏览

超载

重载是指在同一范围内有多个具有相同名称但签名不同的方法。

//Overloading
public class test
{
public void getStuff(int id)
{}
public void getStuff(string name)
{}
}

重写

Overriding is a principle that allows you to change the functionality of a method in a child class.

//Overriding
public class test
{
public virtual void getStuff(int id)
{
//Get stuff default location
}
}


public class test2 : test
{
public override void getStuff(int id)
{
//base.getStuff(id);
//or - Get stuff new location
}
}
  • 重载 = 多个方法签名,方法名称相同
  • 重写 = 相同的方法签名(声明为虚) ,在子类中实现

精明的面试官会接着说:

What's the difference between overriding and shadowing?

正如迈克尔所说:

  • 重载 = 多个方法签名,方法名称相同
  • 重写 = 相同的方法签名(声明为虚) ,在子类中实现

还有

  • Shadowing= 如果它使用派生方法,如果作为 BaseClass 它使用基方法。

Shadow = 在派生类中维护两个定义,为了投射基类定义,它会隐藏派生类定义,反之亦然。

方法重载和方法重载是两个完全不同的概念。方法重载具有相同的方法名,但具有不同的签名。方法重写是更改派生类中基类方法的默认实现。下面你可以找到2个优秀的视频教程来解释这些概念。

方法重写 Vs 隐藏

Method overloading

重载和重写的简单定义

重载 (编译时多态性) : 具有相同名称和不同参数的函数

public class A
{
public void print(int x, int y)
{
Console.WriteLine("Parent Method");
}
}


public class B : A
{
public void child()
{
Console.WriteLine("Child Method");
}


public void print(float x, float y)
{
Console.WriteLine("Overload child method");
}
}

重写 (运行时多态性) : : 扩展类中具有与基类中相同名称和相同参数的函数,但具有不同的行为。

public class A
{
public virtual void print()
{
Console.WriteLine("Parent Method");
}
}


public class B : A
{
public void child()
{
Console.WriteLine("Child Method");
}


public override void print()
{
Console.WriteLine("Overriding child method");
}
}

重载是一个概念,其中你有相同的签名或者方法有相同的名字但是有不同的参数和重写,我们有相同的名字方法有不同的参数也有继承称为重写。

还有一点要补充。

重载 多个同名方法。相同或不同的返回类型。不同的参数或不同类型的参数。在同一类或派生类中。

Int Add (int num1,int num2) Int Add (int num1,int num2,int num3) Double Add (int num1,int num2) Double Add (double num1,double num2)

可以在同一个类或派生类中使用。 通常喜欢在同一个类中。例如 Console. WriteLine ()有19个重载方法。

可以重载类构造函数、方法。

Can consider as Compile Time (static / Early Binding) polymorphism.

=====================================================================================================

Overriding cannot be possible in same class. 可以重写类方法、属性、索引器和事件。

有一些限制,比如 重写的基方法必须是虚方法、抽象方法或重写方法。 不能使用新的、静态的或虚拟的修饰符来修改重写方法。

Can Consider as Run Time (Dynamic / Late Binding) polymorphism.

帮助对 http://msdn.microsoft.com/en-us/library/6fawty39.aspx进行版本控制

=====================================================================================================

有用连结

http://msdn.microsoft.com/en-us/library/ms173152.aspx Compile time polymorphism vs. run time polymorphism

Having more than one methods/constructors with same name but different parameters is called overloading. This is a compile time event.

Class Addition
{
int add(int a, int b)
{
return a+b;
}
int add(int a, int b, int c)
{
return a+b+c;
}


public static main (String[] args)
{
Addition addNum = new Addition();
System.out.println(addNum.add(1,2));
System.out.println(addNum.add(1,2,3));
}
}

营业额:

3
6

重写是一个运行时事件,这意味着根据代码在运行时更改输出。

class Car
{
public int topSpeed()
{
return 200;
}
}
class Ferrari extends Car
{
public int topSpeed()
{
return 400;
}
public static void main(String args[])
{
Car car = new Ferrari();
int num= car.topSpeed();
System.out.println("Top speed for this car is: "+num);
}
}

Notice there is a common method in both classes topSpeed(). Since we instantiated a Ferrari, we get a different result.

营业额:

Top speed for this car is: 400

在 C # 中没有像 藏起来了覆盖那样的 Java,没有关键字 重写覆盖方法:

variant 1 without 重写: result is 200

    class Car {
public int topSpeed() {
return 200;
}
}


class Ferrari : Car {
public int topSpeed(){
return 400;
}
}


static void Main(string[] args){
Car car = new Ferrari();
int num= car.topSpeed();
Console.WriteLine("Top speed for this car is: "+num);
Console.ReadLine();
}

variant 2 with override keyword: result is 400

    class Car {
public virtual int topSpeed() {
return 200;
}
}


class Ferrari : Car {
public override int topSpeed(){
return 400;
}
}


static void Main(string[] args){
Car car = new Ferrari();
int num= car.topSpeed();
Console.WriteLine("Top speed for this car is: "+num);
Console.ReadLine();
}

关键字 虚拟的 on Car class 与 期末考试 on Java 相反,意思是 不是最终决定,如果 Car 是抽象的,可以重写或实现它

重载 是静态多态性的一部分,用于实现具有相同名称但不同签名的不同方法。使用 Overriding完成了不完全方法。在我看来,这两个概念之间没有可比性,唯一相似的是,它们都带有相同的词汇,但都已经结束了。

我想和大家分享一个在我学习的过程中很有意义的例子:

这只是一个不包括虚方法或基类的示例。只是提供一个关于主要观点的提示。

假设有一台洗车机,它有一个名为“ Wash”的函数,接受 Car 作为类型。

获取 Car 输入并清洗 Car。

public void Wash(Car anyCar){
//wash the car
}

让我们重载 Wash ()函数

超载:

public void Wash(Truck anyTruck){
//wash the Truck
}

Wash function was only washing a Car before, but now its overloaded to wash a Truck as well.

  • If the provided input object is a Car, it will execute Wash(Car anyCar)
  • 如果提供的输入对象是一个 Truck,那么它将执行 洗车(任何卡车)

让我们重写 Wash ()函数

最重要的是:

public override void Wash(Car anyCar){
//check if the car has already cleaned
if(anyCar.Clean){
//wax the car
}
else{
//wash the car
//dry the car
//wax the car
}
}

清洗功能现在有一个条件,以检查汽车是否已经清洗,不需要再次清洗。

  • 如果车是干净的,那就给它打蜡。

  • 如果不清洁,那么首先洗车,然后烘干,然后打蜡 它

.

所以通过添加新的功能或者做一些完全不同的事情,功能已经被覆盖了。