📄 10.1.3c.htm
字号:
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<title>编译原理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link type="text/css" rel="stylesheet" href="../css/specification.css">
</head>
<body>
<table align="right" width="300">
<tr>
<td>
</imag>
<img src="../images/previous.gif" onmouseover="javascript:style.cursor='hand'"
onclick="vbscript:window.location.href='10.1.1.htm'" ></td>
<td>
<img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'"
onclick="vbscript:window.location.href='10.2.1.htm'" ></imag></td>
</tr>
</table>
<p> </p>
<font class="title2"><b>10.1.3 继承性</b></font>
<table><tr><td>    </td>
<td class="content">
<p>
继承性定义为类A的所有特征并入新的类B中。B可以定义自己的一些其它特征,在一定情况下还可以重新定义(或称为覆盖)从A继承来的方法。如果B继承A,那么类B叫做类A的<font class = "definition2">派生类</font>,而类A叫做类B的<font class = "definition2">基类</font>。</p>
<p>
继承性是面向对象语言最重要的特征之一,继承的层次性允许类库的结构化和引入不同级别的抽象。下面我们用四边形的例子来说明这一点。</p>
<p>
</p>
<font size="3"><font color="#0000FF">class</font> Quadrangle{<br>
<font color="#0000FF">public</font>:<br>
<font color="#0000FF"> int</font> x1,y1,x2,y2,x3,y3,x4,y4;<br>
Quadrangle(<font color="#0000FF">int</font> a1,<font color="#0000FF">int</font> b1,<font color="#0000FF">int</font> a2,<font color="#0000FF">int</font>
b2,<font color="#0000FF">int</font> a3,<font color="#0000FF">int</font> b3,<font color="#0000FF">int</font> a4,<font color="#0000FF">int</font> b4) <br>
{x1=a1;y1=b1;x2=a2;y2=b2;
x3=a3;y3=b3;x4=a4;y4=b4;}<br>
<font color="#0000FF">virtual</font> <font color="#0000FF">double</font> perimeter() <br>
{<br>
<font color="#0000FF">double</font> l1 = sqrt ((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));<br>
<font color="#0000FF">double</font> l2 = sqrt ((x2-x3)*(x3-x3) + (y2-y3)*(y2-y3));<br>
<font color="#0000FF">double</font> l3 = sqrt ((x3-x4)*(x3-x4) + (y3-y4)*(y3-y4));<br>
<font color="#0000FF">double</font> l4 = sqrt ((x4-x1)*(x4-x1) + (y4-y1)*(y4-y1));<br>
<font color="#0000FF"> return</font>
l1+l2+l3+l4;<br>
}<br>
};<br>
<font color="#0000FF">class</font> Rectangle:public Quadrangle{<br>
<font color="#0000FF">double</font> length,width;<br>
<font color="#0000FF">public</font>:<br>
Rectangle(<font color="#0000FF">int</font> a1,<font color="#0000FF">int</font> b1,<font color="#0000FF">int</font> a2,<font color="#0000FF">int</font> b2,<font color="#0000FF">int</font> a3,<font color="#0000FF">int</font> b3,<font color="#0000FF">int</font> a4,<font color="#0000FF">int</font> b4)<br>
:Quadrangle(a1,b1,a2,b2,a3,b3,a4,b4)<br>
{length = sqrt ((a1-a2)*(a1-a2) + (b1-b2)*(b1-b2));<br>
width = sqrt ((a2-a3)*(a3-a3) + (b2-b3)*(b2-b3));<br>
}<br>
<font color="#0000FF">double</font> perimeter() { <font color="#0000FF">return</font> (length+width)*2;}<br>
};<br>
<font color="#0000FF">class</font> Square:public Quadrangle{<br>
<font color="#0000FF">double</font> length;<br>
<font color="#0000FF">public</font>:<br>
Square(<font color="#0000FF">int</font> a1,<font color="#0000FF">int</font> b1,<font color="#0000FF">int</font> a2,<font color="#0000FF">int</font> b2,<font color="#0000FF">int</font> a3,<font color="#0000FF">int</font> b3,<font color="#0000FF">int</font> a4,<font color="#0000FF">int</font> b4)<br>
:Quadrangle(a1,b1,a2,b2,a3,b3,a4,b4)<br>
{length = sqrt ((a1-a2)*(a1-a2) + (b1-b2)*(b1-b2));}<br>
<font color="#0000FF">double</font> perimeter() {<font color="#0000FF">return</font> length*4;}<br>
};<br>
main()<br>
{<br>
Rectangle r(0,0,5,0,5,7,0,7);<br>
Square s(0,0,5,0,5,5,0,5);<br>
Quadrangle *q1, *q2;<br>
q1 = &r;<br>
q2 = &s;<br>
q1 -> perimeter();<br>
q2 -> perimeter();<br>
}<br>
<p align = center></font><b><font size="3">图10.1 一个面向对象的例子</font></b></p>
<p>
程序10.1说明了使用c++定义具有继承关系的类的方法。根据继承关系,四边形是长方形和正方形的基类,每一个四边形(由继承性知道每一个长方形和正方形)包括八个属性--构成四个顶点坐标和一个方法--perimeter。另外,每一个长方形还有两个属性--长和宽;每一个正方形还有一个属性--边长。图1.2给出了四边形的继承层次图。在主程序中,r是属于Rectangle类的对象,s是属于Square类的对象,q1和q2是属于Quadrangle类的指针。由于Rectangle类和Square类都是Quadrangle类的子类,因此我们可以用q1和q2分别指向子类对象s和r。这在继承关系图中也称为<font class = "definition2">向上映射</font>。
</p>
<p>
另外,Rectangle类和Square类通过覆盖基类Quadrangle的方法perimeter,以适应本类局部的特殊需求和环境。由上面可以看出,继承性概念给我们提供了这样的可能性:用简单的办法可以重用部分已有的实现,或扩充它们,还可以重写个别方法。
</p>
<p>
在这个程序中我们还可以看到面向对象语言的一个重要特征:<font class = "definition2">多态性</font>。<font class = "definition2">多态性</font>是指基类的方法调用可以根据实际运行的对象类型不同实现对不同函数体的调用。在c++中多态性是通过虚函数来实现的。虚函数是以virtual关键字开头的函数,象程序10.1中的perimeter就是虚函数。当我们在主程序中调用q1->perimeter()时,就是一种多态性的表现,它会调用类Rectangle中的perimeter方法,而不是Quadrangle中的perimeter方法。
</p>
<p>继承性引入了许多新的编译问题,本章的主要部分就是围绕着继承性的有效实现来考虑的,其它方面只在后面几节作简要的介绍。<p class="MsoNormal" align="center" style="text-align:center;text-indent:21.0pt">
<p align=center><img src="images/10_2.gif" width="347" height="422"></p>
</td></tr></table>
<p> </p>
<table align="right" width="300">
<tr>
<td>
<img src="../images/previous.gif" onmouseover="javascript:style.cursor='hand'"
onclick="vbscript:window.location.href='10.1.1.htm'" ></imag></td>
<td>
<img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'"
onclick="vbscript:window.location.href='10.2.1.htm'" ></imag></td>
</tr>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -