📄 10.1.3.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>
<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.1.3b.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>
</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'" ></td>
<td>
<img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'"
onclick="vbscript:window.location.href='10.1.3b.htm'" ></imag></td>
</tr>
</table>
</body>
</html>
<html><script language="JavaScript">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -