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

📄 inheritancebaseclass..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>Inheritance</title>
</head>

<body background="../basic3.gif">

<table border="0" cellspacing="0" width="596">
  <tr>
    <td>
      <p align="center"><font color="#FF0000" size="4">Inheritance:&nbsp; 
      BaseClass.</font></td>
  </tr>
  <tr>
    <td> 
      <p><font color="#008000">using System;<br>
      <br>
      public class ParentClass<br>
      {<br>
      &nbsp;&nbsp;&nbsp; public ParentClass()<br>
      &nbsp;&nbsp;&nbsp; {<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(&quot;Parent 
      Constructor.&quot;);<br>
      &nbsp;&nbsp;&nbsp; }<br>
      &nbsp;&nbsp;&nbsp; public void print()<br>
      &nbsp;&nbsp;&nbsp; {<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(&quot;I'm a 
      Parent Class.&quot;);<br>
      &nbsp;&nbsp;&nbsp; }<br>
      }<br>
      <br>
      public class ChildClass : ParentClass<br>
      {<br>
      &nbsp;&nbsp;&nbsp; public ChildClass()<br>
      &nbsp;&nbsp;&nbsp; {<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(&quot;Child 
      Constructor.&quot;);<br>
      &nbsp;&nbsp;&nbsp; }<br>
      &nbsp;&nbsp;&nbsp; public static void Main()<br>
      &nbsp;&nbsp;&nbsp; {<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ChildClass child = new 
      ChildClass();<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; child.print();<br>
      &nbsp;&nbsp;&nbsp; }<br>
      }<br>
      <br>
      </font>Output:</td>
  </tr>
  <tr>
    <td><font color="#008000">Parent Constructor.<br>
      Child Constructor.<br>
      I'm a Parent Class.</font></td>
  </tr>
  <tr>
    <td>Listing 8-1 shows two classes.&nbsp; The top class is named ParentClass 
      and the main class is called ChildClass.&nbsp; What we want to do is 
      create a child class, using existing code from ParentClass.</td>
  </tr>
  <tr>
    <td>First we must declare our intention to use ParentClass as the base class 
      of ChildClass.&nbsp; This is accomplished through the ChildClass 
      declaration &quot;public class ChildClass : ParentClass&quot;.&nbsp; The 
      base class is specified by adding a colon, &quot;:&quot;, after the 
      derived class identifier and then specifying the base class name.</td>
  </tr>
  <tr>
    <td>C# supports single class inheritance only.&nbsp; Therefore, you can 
      specify only one base class to inherit from.</td>
  </tr>
  <tr>
    <td>ChildClass has exactly the same capabilities as ParentClass.&nbsp; 
      Because of this, you can also say ChildClass &quot;is&quot; a ParentClass.&nbsp; 
      This is shown in the Main() method of ChildClass when the print() method 
      is called.&nbsp; Child class does not have it's own print() method, so it 
      uses the ParentClass print() method.&nbsp; You can see the results in the 
      3rd line of output.</td>
  </tr>
  <tr>
    <td>Base classes are automatically instantiated before derived classes.&nbsp; 
      Notice the output from Listing 8-1.&nbsp; The ParentClass constructor 
      executed before the ChildClass constructor.</td>
  </tr>
  <tr>
    <td>
      <h5>Listing 8-2.&nbsp; Derived Class Communicating with Base Class:&nbsp; 
      BaseTalk.cs</h5>
    </td>
  </tr>
  <tr>
    <td><font color="#008000">using System;<br>
      <br>
      public class Parent<br>
      {<br>
      &nbsp;&nbsp;&nbsp; string parentString;<br>
      <br>
      &nbsp;&nbsp;&nbsp; public Parent()<br>
      &nbsp;&nbsp;&nbsp; {<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(&quot;Parent 
      Constructor.&quot;);<br>
      &nbsp;&nbsp;&nbsp; }<br>
      <br>
      &nbsp;&nbsp;&nbsp; public Parent(string myString)<br>
      &nbsp;&nbsp;&nbsp; {<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; parentString = myString;<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(parentString);<br>
      &nbsp;&nbsp;&nbsp; }<br>
      <br>
      &nbsp;&nbsp;&nbsp; public void print()<br>
      &nbsp;&nbsp;&nbsp; {<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(&quot;I'm a 
      Parent Class.&quot;);<br>
      &nbsp;&nbsp;&nbsp; }<br>
      }<br>
      <br>
      public class Child : Parent<br>
      {<br>
      &nbsp;&nbsp;&nbsp; public Child() : base(&quot;From Derived&quot;)<br>
      &nbsp;&nbsp;&nbsp; {<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(&quot;Child 
      Constructor.&quot;);<br>
      &nbsp;&nbsp;&nbsp; }<br>
      <br>
      &nbsp;&nbsp;&nbsp; public void print()<br>
      &nbsp;&nbsp;&nbsp; {<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; base.print();<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(&quot;I'm a 
      Child Class.&quot;);<br>
      &nbsp;&nbsp;&nbsp; }<br>
      <br>
      &nbsp;&nbsp;&nbsp; public static void Main()<br>
      &nbsp;&nbsp;&nbsp; {<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Child child = new Child();<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; child.print();<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((Parent)child).print();<br>
      &nbsp;&nbsp;&nbsp; }<br>
      }<br>
      <br>
      </font>Output:</td>
  </tr>
  <tr>
    <td><font color="#008000">From Derived<br>
      Child Constructor.<br>
      I'm a Parent Class.<br>
      I'm a Child Class.<br>
      I'm a Parent Class.</font></td>
  </tr>
  <tr>
    <td>Derived classes can communicate with base classes during instantiation.&nbsp; 
      Listing 8-2 shows how this is done at the child constructor declaration.&nbsp; 
      The colon, &quot;:&quot;, and keyword base call the base class constructor 
      with the matching parameter list.&nbsp; The first line of output shows the 
      base class constructor being called with the string &quot;From 
      Derived&quot;.</td>
  </tr>
  <tr>
    <td>Sometimes you may want to create your own implementation of a method 
      that exists in a base class.&nbsp; The Child class does this by declaring 
      it's own print() method.&nbsp; The Child print() method hides the Parent 
      print method.&nbsp; The effect is the Parent print method will not be 
      called, unless we do something special to make sure it's called.</td>
  </tr>
  <tr>
    <td>Inside the Child print() method, we explicitly call the Parent print() 
      method.&nbsp; This is done by prefixing the method name with 
      &quot;base.&quot;.&nbsp; Using the &quot;base&quot; keyword, you can 
      access any of a base class' public or protected class members.&nbsp; The 
      output from the Child print() method is on output lines 3 and 4.</td>
  </tr>
  <tr>
    <td>Another way to access base class members is through an explicit cast.&nbsp; 
      This is done in the last statement of the Child class Main() method.&nbsp; 
      Remember that a derived class is a specialization of it's base class.&nbsp; 
      This fact allows us to perform a conversion on the derived class, making 
      it an instance of it's base class.&nbsp; The last line of output from 
      Listing 8-2 shows the Parent print() method was indeed executed.</td>
  </tr>
  <tr>
    <td>In summary, you know how to create a derived/base class relationship.&nbsp; 
      You can control instantiation of your base class and call it's methods 
      either implicitly or explicitly.&nbsp; You also understand that a derived 
      class is a specialization of it's base class.</td>
  </tr>
  <tr>
    <td>I invite you to return for Lesson 9:&nbsp; Polymorphism.</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 + -