⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program implementing polymorphism.htm

📁 点net代码20个
💻 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>
      &nbsp;&nbsp;&nbsp; {<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DrawingObject[] dObj = </font><font color="#0000ff" size="2">new</font><font size="2"> 
      DrawingObject[4];<br>
      <br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dObj[0] = </font><font color="#0000ff" size="2">new</font><font size="2"> 
      Line();<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dObj[1] = </font><font color="#0000ff" size="2">new</font><font size="2"> 
      Circle();<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dObj[2] = </font><font color="#0000ff" size="2">new</font><font size="2"> 
      Square();<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dObj[3] = </font><font color="#0000ff" size="2">new</font><font size="2"> 
      DrawingObject();<br>
      </font><font color="#0000ff" size="2"><br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach</font><font size="2"> (DrawingObject 
      drawObj </font><font color="#0000ff" size="2">in</font><font size="2"> 
      dObj)<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      drawObj.Draw();<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
      <br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#0000ff" size="2">return</font><font size="2"> 
      0;<br>
      &nbsp;&nbsp;&nbsp; }<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.&nbsp; This program implements polymorphism.&nbsp; In the 
      Main() method of the DrawDemo class, there is an array being created.&nbsp; 
      The type of object in this array is the DrawingObject class.&nbsp; 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.&nbsp; Because of their inheritance 
      relationship with the DrawingObject class, the Line, Circle, and Square 
      classes can be assigned to the dObj array.&nbsp; Without this capability, 
      you would have to create an array for each type.&nbsp; 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.&nbsp; Within the foreach loop the Draw() method 
      is invoked on each element of the dObj array.&nbsp; Because of 
      polymorphism, the run-time type of each object is invoked.&nbsp; The type 
      of the reference object from the dObj array is a DrawingObject.&nbsp; 
      However, that doesn't matter because the derived classes override the 
      virtual Draw() method of the DrawingObject class.&nbsp; 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.&nbsp; 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.&nbsp; The last line is from the virtual Draw() 
      method of the DrawingObject class.&nbsp; 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.&nbsp; 
      You know how to define a virtual method.&nbsp; You can implement a derived 
      class method that overrides a virtual method.&nbsp; This relationship 
      between virtual methods and the derived class methods that override them 
      enables polymorphism.&nbsp; 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:&nbsp; 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 + -