运行时多态性VS编译时多态 [英] run-time polymorphism VS compile-time polymorphism

查看:57
本文介绍了运行时多态性VS编译时多态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

覆盖是运行时多态性的示例

重载是编译时多态的示例。



我的困惑是:在运行时解决了什么? (重写)

什么是'那个'在编译时解决(重载)



换句话说,什么是runtine在覆盖和什么是重载的complile-time?

我不能在编译时使用覆盖,反之亦然吗?



请注意:我理解运行时和编译时的含义....但无法将它与上述两个语句联系起来。帮助!!!

"Overriding is the example of run-time polymorphism"
"Overloading is the example of compile-time polymorphism."

My confusion is : What is 'that' resolved at runtime ? (overriding)
What is 'that' resolved at compile-time(overloading)

In other words, what is runtine in overriding and what is complile-time in overloading ?
Can't I use overriding at compile-time or vice-versa ?

pLEASE Note : that I understand the meaning of runtime and compile-time....but unable to relate it with the above two statements. HELP !!!

推荐答案

假设方法 void方法(arg a){} void方法(arg a,arg b){} 。如果调用方法(a); ,编译器在分析函数参数后选择 void方法(arg a){} 。运算符重载也会发生同样的事情。



另一方面,由于对象尚未初始化,因此在运行时才能解决覆盖问题。假设一个基类 Vehicle ,其虚拟方法 virtual void Drive(){}; 和派生类 Car Bicycle 并覆盖 Drive()方法。



请参阅以下代码:



Suppose methods void Method(arg a) { } and void Method(arg a, arg b) { }. If you call Method(a);, compiler selects void Method(arg a) { } after analyzing function arguments. Same thing happens with operator overloading.

On the other hand, overriding cannot be resolved until run time since objects haven't initialized yet. Suppose a base class Vehicle with a virtual method virtual void Drive() { }; and derived classes Car, Bicycle with overrided Drive() methods.

See code below:

Vehicle v;

v = new Bicycle();
v.Drive();

v = new Car();
v.Drive();





编译器需要虚拟驱动器方法并且编译成功。 Drive方法的正确版本直到运行时才能确定,因为只有在那时 Vehicle v 分别被初始化为Bicycle和Car。



希望这会有所帮助。



Compiler demands virtual Drive method and it compiles successfully. The right version of Drive method cannot be determined until run-time since since only at that time Vehicle v is initialized as Bicycle and Car respectively.

Hope this helps.


重载没有实现多态性。你的第二个引用是一个虚假的陈述(请来源!)。第一个是真的。



这不是很难验证。

我会在你理解的水平上解释多态性但它会需要几页文本,可能需要您的反馈并回答其他问题。



让我们执行以下操作:首先,让我们关闭重载问题。这很简单。如果重命名所有重载的名称以使其唯一,则根本不会更改您的代码。换句话说,只要编译器可以通过调用它们的方式来解析方法而没有歧义,名称就无关紧要了。有时无法确定隐含哪个重载方法,然后编译显示错误。所有这些都没有多态性。

另请参阅: http://en.wikipedia.org/wiki / Method_overloading [ ^ ],注意:有本文中没有提及多态(自然)。



下一步。要理解多态性,你需要理解4件事:



1)继承: http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) [ ^ ]

2)虚拟方法: http://en.wikipedia.org/wiki/Virtual_method [ ^ ],了解内部结构(有用!)另请参见虚拟方法表: http:// en.wikipedia.org/wiki/Virtual_method_table [ ^ ]

3)延迟装订: http://en.wikipedia.o rg / wiki / Late_binding [ ^ ](不详细足够了,参见本文中的参考文献)

4)最后,多态: http:// en.wikipedia.org/wiki/Polymorphism_(computer_science) [ ^ ]



请不要怕太多的文章。它们提供了非常简短的概念性解释,并且易于阅读(而且难以理解)。另外,他们用几种语言解释这些想法很好,所以,如果你熟悉它们中的一些,它可能会有所帮助。



现在,关于什么是的简短回答在运行时解决了?(重写)。



经过一番思考:在编译期间完成覆盖:通过虚方法表间接调用两个不同类中的虚函数(相对抛出继承) (VMT)。

调用者可以使用在编译期间作为某个父类的变量而知道的变量,但在运行时,实际的类实例是派生类之一(这很好)由于继承)。如果不是VMT(非虚函数),则被调用的方法将是编译时类之一,但由于VMT,在运行时调用的虚方法将是实际运行时引用之一。



因此,虽然所有方法(虚拟或非虚拟)在编译期间都是已知的,但确切地说这些方法只知道运行时 - 后期绑定 。最后需要注意的是:规划面向对象的类需要理解和规划。正式使方法虚拟本身可能不会引入后期绑定或多态。我再次推荐阅读;参见下面的参考文献。
There is no polymorphism achieved with overloading. Your second citation is a false statement (source, please!). First one is true.

This is not too hard to validate.
I would explain polymorphism on the level you would understand but it would take a couple of pages of text and may require your feedback and answering further questions.

Let's do the following: first, let's close an issue with overloading. It's quite simple. If you rename all your overloaded names to make them unique, it will not change your code at all. In other words, names do not matter as soon as a compiler can resolve methods by the way they are called without ambiguity. Sometimes it is not possible to say which overloaded method is implied, then compilation shows an error. There is nothing polymorphic about all that.
See also: http://en.wikipedia.org/wiki/Method_overloading[^], pay attention: there is not mentions of polymorphism in this article (naturally).

For the next step. To understand polymorphism you need to understand 4 things:

1) Inheritance: http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)[^]
2) Virtual methods: http://en.wikipedia.org/wiki/Virtual_method[^], to have an idea of the internals (useful!) see also virtual method table: http://en.wikipedia.org/wiki/Virtual_method_table[^]
3) Late binding: http://en.wikipedia.org/wiki/Late_binding[^] (not detailed enough, see also references in this article)
4) Finally, polymorphism: http://en.wikipedia.org/wiki/Polymorphism_(computer_science)[^]

Please don't afraid of too many articles. They provide pretty short conceptual explanation and are good to read (and not to hard to understand). Also, good thing they explain ideas on several languages, so, if you familiar with some of them it may help.

Now, a short answer on "What is 'that' resolved at runtime ? (overriding)".

After some thinking: overriding itself is done during compile time as well: a virtual function in two different classes (relative throw inheritance) is called indirectly, through Virtual Method Table (VMT).
A caller may work with a variable which is known during compile time as a variable of some parent class, but during run time the actual class instance is one of derived class (which is fine due to inheritance). If not VMT (not virtual function), the called method would be the one of the compile-time class, but due to VMT a virtual method called during run-time will be the one of actual run-time reference.

So, while all the methods (virtual or not) are known during compile time, which exactly is called are known only run-time -- late binding. For a final note: planning object-oriented classes needs understanding and planning. Formally making methods virtual per se may introduce no late binding or polymorphism. Again, I recommend reading; see the references below.


在编译时多元化,函数调用在编译时完成,即函数调用在编译时解析。 ex函数重载



而在运行时多态,函数调用是在运行时完成的,即函数调用在运行时解析。 ex function overriiding。
in compile time polymosphism, function calling is done at compile time i.e function calls is resolved at compile time. ex function overloading

whereas in run time polymorphism, function calling is done at run time i.e function calls is resolved at runtime . ex function overriiding.


这篇关于运行时多态性VS编译时多态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆