C# 编译时重载和运行时重载如何?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10915828/
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
How Overloading is Compile Time and Overriding is Runtime?
提问by Learner
Folks
乡亲
I came across many threads for understanding polymorphism (Both compile time and run time). I was surprised to see some links where the programmers are claiming Overloading is Runtime and Overriding is compile time.
我遇到了许多用于理解多态性的线程(编译时和运行时)。我很惊讶地看到一些链接,其中程序员声称重载是运行时而覆盖是编译时。
What I want to know from here is:
我想从这里知道的是:
- Runtime Polymorphism with a REAL TIME example and small code and what scenario we should use.
- Compile time Polymorphism with REAL TIME example and small code and when to use.
- 带有实时示例和小代码的运行时多态性以及我们应该使用的场景。
- 编译时多态与实时示例和小代码以及何时使用。
Because I read many theoretical definitions, but I am not satisfied in understanding that.
因为我读了很多理论定义,但我对理解并不满意。
Also, I gave a thought, that where I also felt, overloading should be runtime as because, say I have a method that calculates Area, at runtime only it decides which overloaded method to call based on parameters I pass (Say if I pass only one parameter, it should fire Square, and if parameters are 2, it should fire Rectangle)....So isn't it I can claim its runtime ? How its complie time ? (Most say theoretically, overloading is compile time but they dont even give a correct REAL time example...very few claim its runtime)....
另外,我想到了,我也觉得,重载应该是运行时,因为,说我有一个计算面积的方法,在运行时只有它根据我传递的参数决定调用哪个重载方法(假设我只传递一个参数,它应该触发 Square,如果参数是 2,它应该触发 Rectangle)....所以不是我可以声明它的运行时间吗?它的编译时间如何?(大多数人从理论上说,重载是编译时间,但他们甚至没有给出正确的实时示例......很少有人声称其运行时间)......
Also, I feel overriding is compile time because, while you write code and complie, you ensure you used virtual keyword and also overriding that method in derived class which otherwise would give you compile time error. So I feel its compile time, the same way where I saw in a thread.....But most threads claims its runtime :D
此外,我觉得覆盖是编译时间,因为在编写代码和编译时,您确保使用 virtual 关键字并覆盖派生类中的该方法,否则会导致编译时错误。所以我觉得它的编译时间,就像我在线程中看到的一样......但是大多数线程都声称它的运行时间:D
I am confused :( This question is additional to my question 1 and 2. Please help with a real time example.. as I am already aware of theoretical definitions .... :(
我很困惑 :( 这个问题是我的问题 1 和 2 的补充。请帮助提供一个实时示例.. 因为我已经知道理论定义.. :(
Thank you....
谢谢....
采纳答案by Jon Senchyna
In the case of Overloading, you are using static (compile-time) polymorphism because the compiler is aware of exactly which method you are calling. For example:
在重载的情况下,您使用的是静态(编译时)多态性,因为编译器确切地知道您正在调用哪个方法。例如:
public static class test
{
static void Main(string[] args)
{
Foo();
Foo("test");
}
public static void Foo()
{
Console.WriteLine("No message supplied");
}
public static void Foo(string message)
{
Console.WriteLine(message);
}
}
In this case, the compiler knows exactly which Foo() method we are calling, based on the number/type of parameters.
在这种情况下,编译器根据参数的数量/类型确切地知道我们正在调用哪个 Foo() 方法。
Overriding is an example of dynamic (runtime) polymorphism). This is due to the fact that the compiler doesn't necessarily know what type of object is being passed in at compile-time. Suppose you have the following classes in a library:
覆盖是动态(运行时)多态的一个例子)。这是因为编译器不一定知道在编译时传入的是什么类型的对象。假设您在库中有以下类:
public static class MessagePrinter
{
public static void PrintMessage(IMessage message)
{
Console.WriteLine(message.GetMessage());
}
}
public interface IMessage
{
public string GetMessage();
}
public class XMLMessage : IMessage
{
public string GetMessage()
{
return "This is an XML Message";
}
}
public class SOAPMessage : IMessage
{
public string GetMessage()
{
return "This is a SOAP Message";
}
}
At compile time, you don't know if the caller of that function is passing in an XMLMessage, a SOAPMessage, or possibly another type of IMessage defined elsewhere. When the PrintMessage() function is call, it determines which version of GetMessage() to use at runtime, based on the type of IMessage that is passed in.
在编译时,您不知道该函数的调用者是否正在传入 XMLMessage、SOAPMessage 或可能在别处定义的其他类型的 IMessage。调用 PrintMessage() 函数时,它会根据传入的 IMessage 类型确定在运行时使用哪个版本的 GetMessage()。
回答by Pranay Rana
Read : Polymorphism (C# Programming Guide)
阅读:多态(C# 编程指南)
Similar answer : Compile Time and run time Polymorphism
类似的答案:编译时和运行时多态性
Well, there are two types of Polymorphism as stated below:
好吧,有两种类型的多态性,如下所述:
- Static Polymorphism (Early binding)
- Dynamic Polymorphism (Late binding)
- 静态多态(早期绑定)
- 动态多态(后期绑定)
Static Polymorphism(Early Binding):
静态多态(早期绑定):
Static Polymorphism is also know as Early Binding and Compile time Polymorphism. Method Overloading and Operator Overloading are examples of the same.
静态多态也称为早期绑定和编译时多态。方法重载和运算符重载是相同的例子。
It is known as Early Binding because the compiler is aware of the functions with same name and also which overloaded function is tobe called is known at compile time.
它被称为早期绑定,因为编译器知道具有相同名称的函数,并且在编译时知道要调用哪个重载函数。
For example:
例如:
public class Test
{
public Test()
{
}
public int add(int no1, int no2)
{
}
public int add(int no1, int no2, int no3)
{
}
}
class Program
{
static void Main(string[] args)
{
Test tst = new Test();
int sum = tst.add(10, 20);
// here in above statement compiler is aware at compile time that need to call function add(int no1, int no2), hence it is called early binding and it is fixed so called static binding.
}
}
Dynamic Polymorphism(Late Binding):
动态多态(后期绑定):
public class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("Animal sound");
}
}
public class Dog:Animal
{
public override void MakeSound()
{
Console.WriteLine("Dog sound");
}
}
class Program
{
static void Main(string[] args)
{
Animal an = new Dog();
an.MakeSound();
Console.ReadLine();
}
}
As in the above code , as any other call to a virtual method, will be compiled to a callvirt IL instruction. This means that the actual method that gets called is determined at run-time (unless the JIT can optimize some special case), but the compiler checked that the method exists, it chose the most appropriate overload (if any) and it has the guarantee that the function pointer will exist at a well-defined location in the vtable of the type (even though that is an implementation detail). The process of resolving the virtual call is extremely fast (you only need to dereference a few pointers), so it doesn't make much of a difference.
与上面的代码一样,对虚拟方法的任何其他调用都将被编译为 callvirt IL 指令。这意味着被调用的实际方法是在运行时确定的(除非 JIT 可以优化某些特殊情况),但是编译器检查了该方法是否存在,它选择了最合适的重载(如果有)并且它有保证函数指针将存在于该类型的 vtable 中一个明确定义的位置(即使这是一个实现细节)。解析虚拟调用的过程非常快(你只需要解引用几个指针),所以没有太大区别。
回答by Usama Qureshi
public class Animal {
public virtual void MakeSound()
{
Console.WriteLine("Animal sound");
} }
public class Dog:Animal {
public override void MakeSound()
{
Console.WriteLine("Dog sound");
} }
class Program {
static void Main(string[] args)
{
Animal an = new Dog();
an.MakeSound();
Console.ReadLine();
} }
this is dynamic polymorphism,since it is decided at runtime which version of MakeSound will be called either of the parent or of the child, as a child may not override the parent function or may override it but all this is decided at runtime, which version is called ....
这是动态多态性,因为它是在运行时决定调用父级或子级的哪个版本的 MakeSound,因为子级可能不会覆盖父函数或可能覆盖它,但这一切都在运行时决定,哪个版本叫做 ....

