C# 编译时多态与运行时多态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2152848/
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
Compile time polymorphism vs. run time polymorphism
提问by Saurabh
Why is overloading called compile time polymorphism and Overriding run time polymorphism in C#?
为什么在 C# 中重载称为编译时多态性和覆盖运行时多态性?
采纳答案by Nate Heinrich
Overridden functions are functions that have the same signature, but are implemented in different derived classes. At compile time, usually the base class type is used to reference an object, though at run time this object could be of a derived type, so when an overridden method is called, the implementation that is called is dependent on what kind of object is doing the calling (base vs. a derived type) which is unknown at compile time.
重载函数是具有相同签名但在不同派生类中实现的函数。在编译时,通常使用基类类型来引用一个对象,虽然在运行时这个对象可能是派生类型,所以当一个覆盖方法被调用时,被调用的实现取决于是什么类型的对象执行在编译时未知的调用(基本类型与派生类型)。
Overloading (not really polymorphism) is simply multiple functions which have the same name but different signatures (think multiple constructors for an object taking different numbers of arguments). Which method is called is known at compile time, because the arguments are specified at this time.
重载(不是真正的多态性)只是多个具有相同名称但不同签名的函数(想想一个对象的多个构造函数采用不同数量的参数)。调用哪个方法在编译时是已知的,因为此时指定了参数。
回答by Max Shawabkeh
Because it's known at compile time which of your overloaded functions is called, but that is not always the case for an overridden function.
因为在编译时就知道调用了哪个重载函数,但对于被覆盖的函数而言,情况并非总是如此。
回答by Jon Skeet
Well, overloading decisions (which method signaturesare used, based on the arguments1) are made by the compiler, whereas overriding decisions (which method implementationsare used, based on the type of the target of the method) are made by the CLR at execution time.
好吧,重载决策(使用哪些方法签名,基于参数1)由编译器做出,而覆盖决策(使用哪些方法实现,基于方法目标的类型)由 CLR 在执行时间处理时间。
I wouldn't usually call overloading "polymorphism" though. In my experience the word usuallyrefers to overriding. I suppose overloading doesallow you to treat an object of one type as another, although overloading itself doesn't need to be involved there - it's just normal type conversions.
不过,我通常不会称重载为“多态”。根据我的经验,这个词通常是指覆盖。我想重载确实允许您将一种类型的对象视为另一种类型,尽管重载本身不需要涉及到那里 - 它只是普通的类型转换。
Here's an example showing that overload choice is performed at compile time:
这是一个示例,显示在编译时执行重载选择:
using System;
class Test
{
static void Foo(object a)
{
Console.WriteLine("Object overload called");
}
static void Foo(string a)
{
Console.WriteLine("String overload called");
}
static void Main()
{
object x = "hello";
Foo(x);
}
}
Here the Foo(object)
overload is called because x
is of type object
at compile time - it's only at execution time that it's known to refer to a string.
在这里Foo(object)
调用重载是因为它x
是object
编译时的类型- 只有在执行时才知道它引用了一个字符串。
Compare that with this example:
与此示例进行比较:
using System;
class Base
{
public virtual void Foo()
{
Console.WriteLine("Base.Foo called");
}
}
class Derived : Base
{
public override void Foo()
{
Console.WriteLine("Derived.Foo called");
}
}
class Test
{
static void Main()
{
Base x = new Derived();
x.Foo();
}
}
Here the compile-timetype of x
is Base
, but it's still the derived class's overriding method which is called, because the execution-timetype of the object that x
refers to is Derived
.
这里的编译时类型x
是Base
,但它仍然是被调用的派生类的覆盖方法,因为引用的对象的执行时类型x
是Derived
。
1It's slightly more complicated than that in fact, due to method hiding etc - but in simple cases you can think of it as just picking the signature.
1由于方法隐藏等原因,它比实际上稍微复杂一些 - 但在简单的情况下,您可以将其视为只是选择签名。
回答by Sergey Teplyakov
Classical examples of static polimorphism are based on template metaprogrammingor Duck Typingbut not on method overloading.
静态多态的经典示例基于模板元编程或Duck Typing,而不是基于方法重载。
Static polimorphism means that desicion is made by compilier (statically), and dynamic polimorphism means that desition is made only in runtime (dynamically).
静态多态意味着决策是由编译器(静态)进行的,而动态多态意味着决策仅在运行时(动态)进行。
回答by MSV Muthu
compile time polymorphism
编译时多态
Suppose lets say you have 2 methods as follows; since the method shares same name but have different parameters; it is called as "overloaded" method. Eat(string food); Eat(string food, string SpoonOrFork);
假设您有以下两种方法;由于该方法具有相同的名称但具有不同的参数;它被称为“重载”方法。吃(串食);吃(串食物,串 SpoonOrFork);
and you are using like this in your dinner class
你在你的晚餐课上是这样使用的
public class Man
{
public bool Eat (string food)
{
//implementation
}
public bool Eat (string food, string SpoonOrFork)
{
//implementation
}
}
public class dinner
{
public bool Start()
{
string food = "course1";
Man.Eat ( food);
}
}
Now when you compile this program the compiler knows exactly which version of Eat method to call during compile time itself (because of the difference in parameters).
现在,当您编译此程序时,编译器会确切地知道在编译时调用哪个版本的 Eat 方法(因为参数不同)。
That's why it is called as compile time polymorphism.
这就是为什么它被称为编译时多态性。
Run time polymorphism
运行时多态
public class chimp
{
public virtual void walk()
{
Console.WriteLine("I am walking using 4 legs");
}
}
public class neanderthals : chimp
{
public override void walk()
{
Console.WriteLine("I am walking using 2 legs");
}
}
class Program
{
static void Main(string[] args)
{
chimp x = new neanderthals();
x.walk();
Console.ReadLine(); // this will give an output of "I am walking using 2 legs"
}
}
In the above code xis of type chimp. Even though the compiler thinks it is going to call the walk method in chimp; but that is not what actually happens. Since it depends on CLR (run time) this kind of polymorphism is called "run-time" polymorphism.
在上面的代码中,x是chimp类型。即使编译器认为它会在 chimp 中调用 walk 方法;但实际情况并非如此。由于它依赖于 CLR(运行时),因此这种多态性称为“运行时”多态性。
回答by Jom George
Polymorphism
多态性
Through inheritance, a class can be used as more than one type; it can be used as its own type, any base types, or any interface type if it implements interfaces. This is called polymorphism.
通过继承,一个类可以作为多个类型使用;如果它实现了接口,它可以用作它自己的类型、任何基类型或任何接口类型。这称为多态性。
Polymorphism means having more than one form. Overloading and overriding are used to implement polymorphism. Polymorphism is classified into compile time polymorphism or early binding or static binding and Runtime polymorphism or late binding or dynamic binding.
多态性意味着具有不止一种形式。重载和覆盖用于实现多态。多态性分为编译时多态性或早期绑定或静态绑定和运行时多态性或后期绑定或动态绑定。
Overriding - same method names with same arguments and same return types associated in a class and its subclass. Overriding in C# makes use of the "override" keyword. To override a method means to replace it with a new way of handling data.
覆盖 - 在类及其子类中关联的具有相同参数和相同返回类型的相同方法名称。C# 中的覆盖使用了“override”关键字。覆盖一个方法意味着用一种新的处理数据的方式来替换它。
Overloading - same method name with different arguments, may or may not be same return type written in the same class itself.
重载 - 具有不同参数的相同方法名称,可能是也可能不是写在同一类本身中的相同返回类型。
Compile time Polymorphism or Early Binding
编译时多态或早期绑定
The polymorphism in which compiler identifies which polymorphic form it has to execute at compile time it self is called as compile time polymorphism or early binding.
编译器确定它必须在编译时执行哪种多态形式的多态性称为编译时多态性或早期绑定。
Advantage of early binding is execution will be fast. Because every thing about the method is known to compiler during compilation it self and disadvantage is lack of flexibility.
早期绑定的优点是执行速度很快。因为在编译过程中编译器知道关于该方法的每一件事,它的缺点是缺乏灵活性。
Examples of early binding are overloaded methods, overloaded operators and overridden methods that are called directly by using derived objects.
早期绑定的示例是重载方法、重载运算符和使用派生对象直接调用的重载方法。
Runtime Polymorphism or Late Binding
运行时多态或后期绑定
The polymorphism in which compiler identifies which polymorphic form to execute at runtime but not at compile time is called as runtime polymorphism or late binding.
编译器确定在运行时而不是在编译时执行哪种多态形式的多态称为运行时多态或后期绑定。
Advantage of late binding is flexibility and disadvantage is execution will be slow as compiler has to get the information about the method to execute at runtime.
后期绑定的优点是灵活性,缺点是执行速度会很慢,因为编译器必须在运行时获取有关要执行的方法的信息。
Example of late binding is overridden methods that are called using base class object.
后期绑定的示例是使用基类对象调用的重写方法。
class A
{
public virtual void Leg(string Name)
{
}
}
class B:A
{
public override void Leg(string Name)
{
}
}
Example for Over loading
过载示例
class A
{
void a()
{
}
void a(string Name)
{
}
}
In other words, "Many forms of a single object is called Polymorphism."
换句话说,“单个对象的多种形式称为多态性”。
Eg:
例如:
A Team Leader behaves to Sub Ordinate. A Team Leader behaves to his/her seniors. A Team Leader behaves to other Team Leaders.
团队领导的行为要服从下属。团队领导对他/她的前辈表现得很好。团队领导对其他团队领导的表现。
Here Team Leader is an object but attitude is different in different situation.
这里Team Leader是一个对象,但在不同的情况下态度是不同的。
Difference between Method Overriding and Method hiding
方法覆盖和方法隐藏之间的区别
Method overriding allows a subclass to provide a specific implementation of a method that is already provided by base class. The implementation in the subclass overrides (replaces) the implementation in the base class. The important thing to remember about overriding is that the method that is doing the overriding is related to the method in the base class. When a virtual method is called on a reference, the actual type of the object to which the reference refers is used to determine which method implementation should be used. When a method of a base class is overridden in a derived class (subclass), the version defined in the derived class is used. This is so even should the calling application be unaware that the object is an instance of the derived class.
方法覆盖允许子类提供基类已经提供的方法的特定实现。子类中的实现覆盖(替换)基类中的实现。关于覆盖要记住的重要一点是,执行覆盖的方法与基类中的方法相关。当对引用调用虚拟方法时,引用所引用的对象的实际类型用于确定应使用哪种方法实现。当基类的方法在派生类(子类)中被覆盖时,将使用派生类中定义的版本。即使调用应用程序不知道该对象是派生类的实例,也是如此。
Method hiding does not have a relationship between the methods in the base class and derived class. The method in the derived class hides the method in the base class.
方法隐藏在基类和派生类中的方法之间没有关系。派生类中的方法隐藏了基类中的方法。
回答by Adiii
Compile time Polymorphism
编译时多态
Compile time Polymorphism is also known as method overloading. Method overloading means having two or more methods with the same name but with different signatures.
编译时多态也称为方法重载。方法重载意味着具有两个或多个名称相同但签名不同的方法。
Run time Polymorphism
运行时多态
Run time Polymorphism is also known as method overriding. Method overriding means having two or more methods with the same name and same signature, but with a different implementation
运行时多态也称为方法覆盖。方法覆盖意味着有两个或多个具有相同名称和相同签名的方法,但具有不同的实现
回答by tarique sultan
Polymorphism
多态性
Polymorphism means many forms (ability to take more than one form). In Polymorphism poly means “multiple” and morph means “forms” so polymorphism means many forms.
多态性意味着多种形式(采用一种以上形式的能力)。在多态性中,poly 的意思是“多个”,而 morph 的意思是“形式”,所以多态性意味着多种形式。
In polymorphism we will declare methods with same name and different parameters in same class or methods with same name and same parameters in different classes. Polymorphism has ability to provide different implementation of methods that are implemented with same name.
在多态中,我们会在同一个类中声明同名不同参数的方法,或者在不同类中声明同名同参数的方法。多态性能够提供以相同名称实现的方法的不同实现。
In Polymorphism we have 2 different types those are
在多态性中,我们有 2 种不同的类型,它们是
- Compile Time Polymorphism (Called as Early Binding or Overloading or static binding)
- Run Time Polymorphism (Called as Late Binding or Overriding or dynamic binding)
Compile Time Polymorphism
编译时多态
Compile time polymorphism means we will declare methods with same name but different signatures because of this we will perform different tasks with same method name. This compile time polymorphism also called as early binding or method overloading.
编译时多态意味着我们将声明名称相同但签名不同的方法,因此我们将使用相同的方法名称执行不同的任务。这种编译时多态性也称为早期绑定或方法重载。
Method Overloading or compile time polymorphism means same method names with different signatures (different parameters)
方法重载或编译时多态意味着具有不同签名(不同参数)的相同方法名称
For more details check this link polymorphism in c#
有关更多详细信息,请查看 c# 中的此链接多态性
Run Time Polymorphism
运行时多态
Run time polymorphism also called as late binding or method overriding or dynamic polymorphism. Run time polymorphism or method overriding means same method names with same signatures.
运行时多态也称为后期绑定或方法覆盖或动态多态。运行时多态性或方法覆盖意味着具有相同签名的相同方法名称。
In this run time polymorphism or method overriding we can override a method in base class by creating similar function in derived class this can be achieved by using inheritance principle and using “virtual & override” keywords.
在这个运行时多态性或方法覆盖中,我们可以通过在派生类中创建类似的函数来覆盖基类中的方法,这可以通过使用继承原则和使用“virtual & override”关键字来实现。
回答by Prasad Mitkari
Run time Polymorphism Example in c#.
c# 中的运行时多态性示例。
using System;
public class demo{
public static void Main(String[] args){
cal cal ;
add a = new add();
cal = a;
Console.WriteLine("Addition is" + cal.calculate(20, 20));
sub s = new sub();
cal = s;
Console.WriteLine("Substraction is" + cal.calculate(20, 20));
mul m = new mul();
cal = m;
Console.WriteLine("Multiplication is" + cal.calculate(20, 20));
div d = new div();
cal = d;
Console.WriteLine("Division is" + cal.calculate(20, 20));
Console.ReadLine();
}
}
public abstract class cal{
public abstract int calculate(int a, int b);
}
public class add : cal {
public override int calculate(int a ,int b){
return a+b;
}
}
public class sub : cal{
public override int calculate(int a, int b){
return a-b;
}
}
public class mul : cal{
public override int calculate(int a, int b){
return a*b;
}
}
public class div : cal{
public override int calculate(int a, int b){
return a/b;
}
}