什么是多态? [英] What is polymorphism?

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

问题描述

我必须写一篇关于面向对象编程的论文,我正在做一些阅读,以确保我理解它。在我正在阅读的一本书中,

然而,多态性定义为:


两个不同对象响应同一请求的能力

消息以他们自己独特的方式


我以为它是:


的能力

独特方式回复不同消息的同一对象 


本书中的示例似乎支持了我的理解。

这是书中的错误还是我不理解?

I have to write a paper about object oriented programming and I''m doing
some reading to make sure I understand it. In a book I''m reading,
however, polymorphism is defined as:

"the ability of two different objects to respond to the same request
message in their own unique way"

I thought that it was:

"the ability of same object to respond to different messages in
unique ways"

The examples in the book that follow seem to support my understanding.
Is this an error in the book or am I not understanding it?

推荐答案

Seigfried写道:
Seigfried wrote:
我必须写一篇关于面向对象编程的论文,我正在做一些阅读以确保我理解它。在我正在阅读的一本书中,然而,多态性被定义为:

两个不同对象在自己的消息中响应同一请求的能力独特的方式

我认为它是:

同一个对象以独特的方式回应不同信息的能力
<以下书中的例子似乎支持了我的理解。
这是书中的错误还是我不理解?
I have to write a paper about object oriented programming and I''m doing
some reading to make sure I understand it. In a book I''m reading,
however, polymorphism is defined as:

"the ability of two different objects to respond to the same request
message in their own unique way"

I thought that it was:

"the ability of same object to respond to different messages in
unique ways"

The examples in the book that follow seem to support my understanding.
Is this an error in the book or am I not understanding it?




你的书是对的。请参阅以下常见问题解答:

http://www.parashift.com/c++-faq-lit...functions.html


干杯! --M



Your book is right. See these FAQs:

http://www.parashift.com/c++-faq-lit...functions.html

Cheers! --M


好的......我得到了静态打字与动态打字相对(你的链接中唯一的

引用多态性)但是我不认为这个问题是答案

。在我看来,调用的函数被选择的方式仍然是一个不同的签名(即,使用

书的语言,请求消息)。你的引用似乎唯一可能是
演示的是,被调用的函数可能反过来,

调用另一个函数。从逻辑上讲,这可以被认为是

只是两种方式调用相同的函数。


我不想看起来忘恩负义,但是人们非常普遍地通过看到这个链接......来解释

所有东西来回答问题。我正在寻找的是为什么这个链接解释

所有东西。


mlimber写道:
OK ... I get the "static typing" versus "dynamic typing" (the only
reference to polymorphism in your link) but I don''t think that answers
the question. It seems to me that the way the invoked function is
selected remains a different signature (that is, in the language of the
book, "request message"). The only thing that your reference seems to
demonstrate is that it''s possible for an invoked function to, in turn,
invoke another function. In logical terms, this can be thought of as
simply two ways of invoking the same function.

I don''t want to seem ungrateful, but it''s awfully common for people to
simply respond to questions with "see this link ... it explains
everything". What I''m looking for is WHY does this link "explain
everything".

mlimber wrote:
Seigfried写道:
Seigfried wrote:
我必须写一篇关于面向对象编程的文章,我正在做
...< snip> ...
I have to write a paper about object oriented programming and I''m doing
... <snip> ...


请参阅以下常见问题解答:

http://www.parashift.com/c++-faq-lit...functions.html

干杯! --M


See these FAQs:

http://www.parashift.com/c++-faq-lit...functions.html

Cheers! --M






Seigfried写道:
Seigfried wrote:
我必须写一篇关于面向对象的论文编程和我正在做一些阅读以确保我理解它。在我正在阅读的一本书中,然而,多态性被定义为:

两个不同对象在自己的消息中响应同一请求的能力独特的方式

我认为它是:

相同对象以独特方式响应不同消息的能力


这是程序编程。见证:


struct Foo {...};

void Bar(Foo * pFoo,int q);

void Baz(Foo * pFoo,double n);


Bar()和Baz()在概念上是Foo的方法。调用语法

Bar(pFoo,42)是无关紧要的。 pFoo-> Bar(42)将是语法糖。


所以这里Foo响应Bar"消息"不同于它回应

Baz()消息。


现在考虑(用假设的C语言):


struct Frob {...};

struct Glob:Frob {...};

void Bar(Frob * pFrob,int q);

void Bar(Glob * pGlob,int q);

Glob aGlob;

Frob * pFrob =& aGlob;

Bar(pFrob,42);


每个版本的Bar()都有不同之处。一个在Frobs上工作,另一个在Globs上工作。但是,在这种神奇的语言中,Bar消息确实在编译时没有绑定到Bar方法。这意味着编译器确实没有看到bar(pFrob,42),只看到Frob *类型,并选择void Bar(Frob

* pFrob,int q) 。


相反,编译器会添加等待运行时的额外代码。该代码

检测到pFrob *指向Glob。因此,在运行时,围绕Bar(pFrob,42)的代码

可能仍然无法正确输入类型。这可以节省很多纠结的if语句中的代码来检测事物的真实类型。


所以在C ++中:


Frob& aFrob = getObject();

aFrob.Bar(42);


点。知道在运行时选择哪个Bar()。

后面的书中的例子似乎支持我的理解。
这是书中的错误还是我不理解?
I have to write a paper about object oriented programming and I''m doing
some reading to make sure I understand it. In a book I''m reading,
however, polymorphism is defined as:

"the ability of two different objects to respond to the same request
message in their own unique way"

I thought that it was:

"the ability of same object to respond to different messages in
unique ways"
That is procedural programming. Witness:

struct Foo { ... };
void Bar(Foo * pFoo, int q);
void Baz(Foo * pFoo, double n);

Bar() and Baz(), conceptually, are methods of Foo. The calling syntax,
Bar(pFoo, 42), is irrelevant. pFoo->Bar(42) would be syntactic sugar.

So here Foo responds to the Bar "message" differently than it responds to
the Baz() "message".

Now consider (in a hypothetical C-like language):

struct Frob { ... };
struct Glob: Frob { ... };
void Bar(Frob * pFrob, int q);
void Bar(Glob * pGlob, int q);
Glob aGlob;
Frob * pFrob = &aGlob;
Bar(pFrob, 42);

Each version of Bar() does something different. One works on Frobs, the
other on Globs. However, in this magical language, the Bar message does
not bind to a Bar method at compile time. That means the compiler does
not look at Bar(pFrob, 42), see only a Frob* type, and pick void Bar(Frob
* pFrob, int q).

Instead, the compiler adds extra code that waits until runtime. That code
detects that pFrob* reeeeally points to a Glob. So at runtime the code
around Bar(pFrob, 42) can remain oblivious to the correct type. That saves
the code from a lot of tangled ''if'' statements detecting things true types.

So in C++:

Frob & aFrob = getObject();
aFrob.Bar(42);

The dot . knows which Bar() to pick, at runtime.
The examples in the book that follow seem to support my understanding.
Is this an error in the book or am I not understanding it?




在这里报告这本书的名字,并揭示其中一个例子,并在你完成这个之前阅读

/设计模式/纸。它显示了
使用中的多态性,解决了实际问题,不像我们提到的一些书籍......


-

Phlip



Report the book''s name here, and reveal one of the examples, and read
/Design Patterns/ before you finish this paper. It shows polymorphism in
use, solving real problems, unlike some books we could mention...

--
Phlip


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

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