📄 program implementing polymorphism.htm
字号:
<html>
<head>
<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>Program Implementing Polymorphism</title>
</head>
<body background="../basic3.gif">
<p align="center"><font color="#FF0000" size="4"><b>Program Implementing
Polymorphism</b></font></p>
<table border="0" cellspacing="0" width="596">
<tr>
<td><font color="#0000ff" size="2">using</font><font size="2"> System;<br>
<br>
</font><font color="#0000ff" size="2">public</font><font size="2"> </font><font color="#0000ff" size="2">class</font><font size="2">
DrawDemo<br>
{<br>
</font><font color="#0000ff" size="2">public</font><font size="2"> </font><font color="#0000ff" size="2">static</font><font size="2">
</font><font color="#0000ff" size="2">int</font><font size="2"> Main(</font><font color="#0000ff" size="2">string</font><font size="2">[]
args)<br>
{<br>
DrawingObject[] dObj = </font><font color="#0000ff" size="2">new</font><font size="2">
DrawingObject[4];<br>
<br>
dObj[0] = </font><font color="#0000ff" size="2">new</font><font size="2">
Line();<br>
dObj[1] = </font><font color="#0000ff" size="2">new</font><font size="2">
Circle();<br>
dObj[2] = </font><font color="#0000ff" size="2">new</font><font size="2">
Square();<br>
dObj[3] = </font><font color="#0000ff" size="2">new</font><font size="2">
DrawingObject();<br>
</font><font color="#0000ff" size="2"><br>
foreach</font><font size="2"> (DrawingObject
drawObj </font><font color="#0000ff" size="2">in</font><font size="2">
dObj)<br>
{<br>
drawObj.Draw();<br>
}<br>
<br>
</font><font color="#0000ff" size="2">return</font><font size="2">
0;<br>
}<br>
}</font></td>
</tr>
<tr>
<td>Listing 9-3 shows a program that uses the classes defined in Listing 9-1
and Listing 9-2. This program implements polymorphism. In the
Main() method of the DrawDemo class, there is an array being created.
The type of object in this array is the DrawingObject class. The
array is named dObj and is being initialized to hold four objects of type
DrawingObject.</td>
</tr>
<tr>
<td>Next the dObj array is initialized. Because of their inheritance
relationship with the DrawingObject class, the Line, Circle, and Square
classes can be assigned to the dObj array. Without this capability,
you would have to create an array for each type. Inheritance allows
derived objects to act like their base class, which saves work.</td>
</tr>
<tr>
<td>After the array is initialized, there is a foreach loop that looks at
each element of the array. Within the foreach loop the Draw() method
is invoked on each element of the dObj array. Because of
polymorphism, the run-time type of each object is invoked. The type
of the reference object from the dObj array is a DrawingObject.
However, that doesn't matter because the derived classes override the
virtual Draw() method of the DrawingObject class. This makes the
overriden Draw() methods of the derived classes execute when the Draw()
method is called using the DrawingObject base class reference from the
dObj array. Here's what the output looks like:</td>
</tr>
<tr>
<td>Output:</td>
</tr>
<tr>
<td>I'm a Line.<br>
I'm a Circle.<br>
I'm a Square.<br>
I'm just a generic drawing object.</td>
</tr>
<tr>
<td>The override Draw() method of each derived class executes as shown in
the DrawDemo program. The last line is from the virtual Draw()
method of the DrawingObject class. This is because the actual
run-time type of the fourth array element was a DrawingObject object.</td>
</tr>
<tr>
<td>In summary, you should now have a basic understanding of polymorphism.
You know how to define a virtual method. You can implement a derived
class method that overrides a virtual method. This relationship
between virtual methods and the derived class methods that override them
enables polymorphism. This lesson showed how to use this
relationship between classes to implement polymorphism in a program.</td>
</tr>
<tr>
<td>I invite you to return for Lesson 10: Properties.</td>
</tr>
<tr>
<td>Your feedback is very important and I appreciate any constructive
contributions you have. Please feel free to contact me for any questions
or comments you may have about this lesson.</td>
</tr>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -