C# 重载和覆盖
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/673721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Overloading and overriding
提问by
What is the difference between overloading and overriding.
重载和覆盖有什么区别。
回答by cgreeno
Overloading
超载
Overloading is when you have multiple methods in the same scope, with the same name but different signatures.
重载是指在同一范围内有多个方法,名称相同但签名不同。
//Overloading
public class test
{
public void getStuff(int id)
{}
public void getStuff(string name)
{}
}
Overriding
覆盖
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
}
}
回答by Michael Meadows
- Overloading = Multiple method signatures, same method name
- Overriding = Same method signature (declared virtual), implemented in sub classes
- 重载 = 多个方法签名,相同的方法名称
- 覆盖 = 相同的方法签名(声明为虚拟),在子类中实现
An astute interviewer would have followed up with:
一个精明的面试官会跟进:
回答by Migol
As Michael said:
正如迈克尔所说:
- Overloading = Multiple method signatures, same method name
- Overriding = Same method signature (declared virtual), implemented in sub classes
- 重载 = 多个方法签名,相同的方法名称
- 覆盖 = 相同的方法签名(声明为虚拟),在子类中实现
and
和
- Shadowing = If treated as DerivedClass it used derived method, if as BaseClass it uses base method.
- Shadowing = 如果被视为 DerivedClass 则使用派生方法,如果作为 BaseClass 则使用 base 方法。
回答by ramya choudary
shadowing = maintains two definitions at derived class and in order to project the base class definition it shadowes(hides)derived class definition and vice versa.
shadowing = 在派生类中维护两个定义,为了投影基类定义,它隐藏(隐藏)派生类定义,反之亦然。
回答by Suresh
Method overloading and Method overriding are 2 different concepts completely different. Method overloading is having the same method name but with different signatures. Method overriding is changing the default implementation of base class method in the derived class. Below you can find 2 excellent video tutorials explaining these concepts.
方法重载和方法覆盖是两个完全不同的概念。方法重载具有相同的方法名称但具有不同的签名。方法覆盖正在改变派生类中基类方法的默认实现。您可以在下面找到 2 个解释这些概念的优秀视频教程。
回答by Devrath
Simple definitions for overloading and overriding
重载和覆盖的简单定义
Overloading(Compile Time Polymorphism):: Functions with same name and different parameters
重载(编译时多态):: 具有相同名称和不同参数的函数
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");
}
}
Overriding(Run Time Polymorphism):: Functions in the extended class with same name and same parameters as in the base class, but with different behaviors.
覆盖(运行时多态):: 扩展类中的函数与基类中具有相同名称和相同参数,但具有不同的行为。
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");
}
}
回答by waqas ur Rehman
Overloading is the concept in which you have same signatures or methods with same name but different parameters and overriding, we have same name methods with different parameters also have inheritance is known as overriding.
重载是指具有相同签名或具有相同名称但不同参数和覆盖的方法,我们具有不同参数的同名方法也具有继承称为覆盖。
回答by Sai
Another point to add.
还有一点要补充。
OverloadingMore than one method with Same name. Same or different return type. Different no of parameters or Different type of parameters. In Same Class or Derived class.
重载多个具有相同名称的方法。相同或不同的返回类型。不同的参数数量或不同类型的参数。在同一类或派生类中。
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)
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)
Can be possible in same class or derived class. Generally prefers in same class. E.g. Console.WriteLine() has 19 overloaded methods.
可以在同一个类或派生类中使用。一般更喜欢在同一个班级。例如 Console.WriteLine() 有 19 个重载方法。
Can overload class constructors, methods.
可以重载类构造函数、方法。
Can consider as Compile Time (static / Early Binding) polymorphism.
可以认为是编译时(静态/早期绑定)多态。
=====================================================================================================
================================================== ================================================== =
Overridingcannot be possible in same class. Can Override class methods, properties, indexers, events.
在同一个类中无法覆盖。可以覆盖类方法、属性、索引器、事件。
Has some limitations like The overridden base method must be virtual, abstract, or override. You cannot use the new, static, or virtual modifiers to modify an override method.
有一些限制,例如被覆盖的基本方法必须是虚拟的、抽象的或覆盖的。不能使用 new、static 或 virtual 修饰符来修改覆盖方法。
Can Consider as Run Time (Dynamic / Late Binding) polymorphism.
可以认为是运行时(动态/后期绑定)多态性。
Helps in versioning http://msdn.microsoft.com/en-us/library/6fawty39.aspx
有助于版本控制http://msdn.microsoft.com/en-us/library/6fawty39.aspx
=====================================================================================================
================================================== ================================================== =
Helpful Links
有用的网址
http://msdn.microsoft.com/en-us/library/ms173152.aspxCompile time polymorphism vs. run time polymorphism
http://msdn.microsoft.com/en-us/library/ms173152.aspx编译时多态与运行时多态
回答by Matt
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));
}
}
O/p:
开/关:
3
6
Overriding is a run time event, meaning based on your code the output changes at run time.
覆盖是一个运行时事件,这意味着根据您的代码,输出在运行时会发生变化。
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.
请注意,topSpeed() 两个类中都有一个通用方法。由于我们实例化了一辆法拉利,我们得到了不同的结果。
O/p:
开/关:
Top speed for this car is: 400
回答by Nurlan
in C# there is no Java like hiddenoverride, without keyword overrideon overriding method! see these C# implementations:
在 C# 中,没有像隐藏覆盖的Java ,覆盖方法上没有关键字覆盖!请参阅这些 C# 实现:
variant 1 without override: result is 200
没有覆盖的变体 1 :结果是 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 overridekeyword: result is 400
带有覆盖关键字的变体 2 :结果为 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();
}
keyword virtualon Car class is opposite for finalon Java, means not final, you can override, or implement if Car was abstract
Car 类上的关键字virtual与Java 上的final相对,意味着不是 final,如果 Car 是抽象的,您可以覆盖或实现