📄 right6-1.htm
字号:
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>第 6 章</title>
<base target="rbottom">
<style>
<!--
.右标题 { font-size: 10pt; color: #000080; text-indent: 0; margin: 0 }
.右内容 { font-size: 10pt; text-align: left; text-indent: 0; line-height: 100%; margin:
0 }
-->
</style>
</head>
<body>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><span style="position: absolute; left: 4; top: -8"><font size="2"><img border="0" src="1.gif" width="63" height="70"></font></span></p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"> </font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left"><font size="2">
<span style="background-color: #CCFF99">第 6 章 </span>><span style="background-color: #CCFF99"> 第 1 节 </span>><span style="background-color: #CCFF99">
6.1 多态性 </span></font></p>
<hr color="#008000" size="1">
<p style="line-height: 150%; text-indent: 0; margin: 0" class="右标题" align="left"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">多态性、封装性和继承性构成了面向对象程序设计的三大特性。</font></p>
<p class="右标题" style="line-height: 150%; margin-top: 0; margin-bottom: 0"><span style="font-family:宋体;mso-ascii-font-family:"Times New Roman";
mso-hansi-font-family:"Times New Roman""><font size="2">多态性是指发出<u>同样的消息</u>被不同类型的对象接收时导致完全<u>不同的行为</u>。即对应相同的函数名,却执行了不同的函数体。这种把函数名与其具体的实现代码相联系的过程,称为联编。</font></span><font size="2"><!--[if gte vml 1]><v:line id="_x0000_s1026" style='position:absolute;
left:0;text-align:left;flip:y;z-index:1' from="117pt,0" to="117pt,15.6pt"
coordsize="21600,21600">
<v:stroke endarrow="block"/>
</v:line><v:line id="_x0000_s1027" style='position:absolute;left:0;
text-align:left;flip:y;z-index:2' from="324pt,0" to="324pt,15.6pt"
coordsize="21600,21600">
<v:stroke endarrow="block"/>
</v:line><![endif]-->
</font>
</p>
<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"> </p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">按照联编进行的阶段的不同,可以分为静态联编和动态联编。这两种联编过程分别对应着多态的两种实现方式:编译时多态、运行时多态。</font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font face="??ì?,SimSun" lang="ZH-CN" size="2">1.编译时多态</font><font size="2"><font face="??ì?,SimSun" lang="ZH-CN">
</font></font><b><span style="mso-bidi-font-size: 12.0pt; font-family: 宋体; mso-bidi-font-family: Times New Roman; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"><font color="#008000" size="4">←</font></span></b><font size="2">
<font FACE="??ì?,SimSun" LANG="ZH-CN">静态联编</font></font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 10"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">静态联编是在程序开始运行之前进行。在编译、连接过程中,系统就可以根据类型匹配等特征确定某一个同名标识到底是要调用哪一段程序代码。</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font color="#FF6600">编译时</font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2"><font color="#FF6600">多态通过函数重载实现。</font>函数重载的两种方式:</font></font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">①</font>
<font FACE="??ì?,SimSun" LANG="ZH-CN">在同一类中重载:同名的成员函数,根据参数个数、类型不同,自动予以区别。</font></font></p>
<font SIZE="3">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 10"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">②</font>
<font FACE="??ì?,SimSun" LANG="ZH-CN">基类成员函数在派生类中的重载。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 10"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">静态联编有时会出现一些意想不到的结果,请看下面的例子。</font></p>
<blockquote>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">#include
<iostream.h></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">class
Point</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">public:</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">Point(double
i, double j) { x=i; y=j; }</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">double
Area( ) const { return 0.0; }</font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">private:</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">double
x, y;</font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">};</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">class
Rectangle:public Point</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">public:</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">Rectangle(double
i, double j, double k, double l);</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">double
Area( ) const { return w*h; }</font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">private:</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">double
w, h;</font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">};</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">Rectangle::Rectangle(double
i, double j, double k, double l):Point(i, j)</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">w=k;
h=l;</font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">}</font><font SIZE="1"> </p>
</font>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">void
main( )</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">Rectangle
rec(3.0, 5.2, 15.0, 25.0);</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">Point
*prtP=&rec;
//声明基类指针,并将基类指针指向派生类对象</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">prtP->Area(
);</font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">}</font></p>
</blockquote>
</blockquote>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">该程序的运行结果为:</font></p>
<blockquote>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">0</font></p>
</blockquote>
</blockquote>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">输出结果表明基类指针ptrP
指向派生类对象,然而此时ptrP->Area( )调用的是基类的Area( )函数,因为通过基类指针只能取得派生类中从基类继承的数据与函数。</font></font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font FACE="oúì?,SimHei" LANG="ZH-CN" size="2">2.
运行时多态</font><span style="mso-bidi-font-size: 12.0pt; font-family: 宋体; mso-bidi-font-family: Times New Roman; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"><font color="#008000" face="oúì?,SimHei" lang="ZH-CN" size="4"><b>←</b></font><font face="oúì?,SimHei" lang="ZH-CN" size="2">动态联编</font></span></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">从上例中可以看出,静态联编时,通过基类指针只能取得派生类中从基类继承的数据与函数。如果我们需要通过基类的指针得到派生类对象中成员函数的调用,能够实现吗?答案是肯定的,<font color="#FF6600">通过虚函数可以达到这个目的,从而实现程序的运行时多态。</font></font></p>
<hr size="1" color="#008000">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"> <span style="position: absolute; left: 32; top: 1216"><a href="right5-3.htm" target="_self"><img border="0" src="rightd1.gif" width="113" height="70"></a></span>
<span style="position: absolute; left: 503; top: 1222"><a href="right6-2.htm" target="_self"><img border="0" src="rightd2.gif" width="124" height="63"></a></span></font></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -