使用多态? [英] The use of polymorphism ?

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

问题描述

多态的唯一用途是使用派生方法的基类方法的相同名称吗?

Is the only use of polymorphism is to use the same name of the base class method for the derived method?

推荐答案





编号多态性是OOP的一个非常强大的功能,使您能够:

1.覆盖派生类中的方法,并从基类调用重写版本对派生的引用存储在基类变量中。

2.子类型之间的交换(IE可以传递给一个函数,例如,需要一个实数,一个整数或者理性的,或非理性的)。这是因为RationalNumber继承自RealNumber。

3.在类型之间进行转换(这部分包括第2点)。



为了更好地理解它,我建议你阅读Akhil Mittal在CodeProject上发表的文章系列: OOP潜水(第1天):多态性和遗传(早期结合/编译时间多态性) [ ^ ]。否则,我不得不在这里写一篇关于它的文章或者真正地综合解释它(我不想这样做,因为这是一个非常重要且非常重要的话题)



另请阅读维基百科 http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29 [ ^ ]



希望这会有所帮助...



Jymmy097
Hi,

No. Polymorphism is a really powerful feature of OOP and enables you to:
1. Override methods in derived classes and call the overridden version from the base class if a reference to the derived one is stored in a base class variable.
2. Exchange between child types (I.E. you can pass to a function which, for instance, needs a real number, an integer number or a rational one, or an irrational one). This because RationalNumber inherits from RealNumber.
3. Making conversions between types (this includes in part the point 2).

In order to understand it better, I advice you to read the serie of articles written by Akhil Mittal published here on CodeProject: Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile Time Polymorphism) [^]. Otherwise I had to write here an article about that or explain it really synthetically (and I do not want to do it as this is a really huge and important topic)

Also read on Wikipedia http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29[^]

Hope this helps...

Jymmy097


请参阅我对该问题的评论。多态性不是你想的。这是一组具有不同运行时类型的对象,可以基于相同的接口进行操作。使用.NET,这样的接口可能是常见的基类,或者某种接口类型(遗憾的是,不太知道的事实,关于接口类型)。



这不是直接相关的成员的名字。 公共接口的成员以多态方式使用,不是因为名称相同,而是因为成员本身是相同的。你能看到区别吗?使用动态调度的一种或另一种机制访问成员: http://en.wikipedia.org/ wiki / Dynamic_dispatch [ ^ ]。



另请参阅: http://en.wikipedia.org/wiki / Polymorphism_%28computer_science%29 [ ^ ]。

另见我过去的答案:

没有过载和过度使用的多态性 [ ^ ],

接口和多态性 [ ^ ]。



-SA
Please see the my comment to the question. The polymorphism is not what you think at all. This is having some set of objects of different runtime types which can be manipulated based on the same interface. With .NET, such interface could be common base class, or some interface type (unfortunately, less known fact, about an interface type).

This is not directly related to "names" of members. The members of common interfaces are uses in a polymorphic way not because the names are the same, but because the members themselves are the same. Can you see the difference? The members are accessed using one or another mechanism of dynamic dispatch: http://en.wikipedia.org/wiki/Dynamic_dispatch[^].

See also: http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29[^].
See also my past answer:
POLYMORPHISM WITHOUT OVERLOADING AND OVERRRIDING IS POSSIBLE[^],
Interfaces and Polymorphism[^].

—SA


这篇关于使用多态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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