📄 inheritancebaseclass..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:
BaseClass.</font></td>
</tr>
<tr>
<td>
<p><font color="#008000">using System;<br>
<br>
public class ParentClass<br>
{<br>
public ParentClass()<br>
{<br>
Console.WriteLine("Parent
Constructor.");<br>
}<br>
public void print()<br>
{<br>
Console.WriteLine("I'm a
Parent Class.");<br>
}<br>
}<br>
<br>
public class ChildClass : ParentClass<br>
{<br>
public ChildClass()<br>
{<br>
Console.WriteLine("Child
Constructor.");<br>
}<br>
public static void Main()<br>
{<br>
ChildClass child = new
ChildClass();<br>
child.print();<br>
}<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. The top class is named ParentClass
and the main class is called ChildClass. 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. This is accomplished through the ChildClass
declaration "public class ChildClass : ParentClass". The
base class is specified by adding a colon, ":", after the
derived class identifier and then specifying the base class name.</td>
</tr>
<tr>
<td>C# supports single class inheritance only. Therefore, you can
specify only one base class to inherit from.</td>
</tr>
<tr>
<td>ChildClass has exactly the same capabilities as ParentClass.
Because of this, you can also say ChildClass "is" a ParentClass.
This is shown in the Main() method of ChildClass when the print() method
is called. Child class does not have it's own print() method, so it
uses the ParentClass print() method. You can see the results in the
3rd line of output.</td>
</tr>
<tr>
<td>Base classes are automatically instantiated before derived classes.
Notice the output from Listing 8-1. The ParentClass constructor
executed before the ChildClass constructor.</td>
</tr>
<tr>
<td>
<h5>Listing 8-2. Derived Class Communicating with Base Class:
BaseTalk.cs</h5>
</td>
</tr>
<tr>
<td><font color="#008000">using System;<br>
<br>
public class Parent<br>
{<br>
string parentString;<br>
<br>
public Parent()<br>
{<br>
Console.WriteLine("Parent
Constructor.");<br>
}<br>
<br>
public Parent(string myString)<br>
{<br>
parentString = myString;<br>
Console.WriteLine(parentString);<br>
}<br>
<br>
public void print()<br>
{<br>
Console.WriteLine("I'm a
Parent Class.");<br>
}<br>
}<br>
<br>
public class Child : Parent<br>
{<br>
public Child() : base("From Derived")<br>
{<br>
Console.WriteLine("Child
Constructor.");<br>
}<br>
<br>
public void print()<br>
{<br>
base.print();<br>
Console.WriteLine("I'm a
Child Class.");<br>
}<br>
<br>
public static void Main()<br>
{<br>
Child child = new Child();<br>
child.print();<br>
((Parent)child).print();<br>
}<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.
Listing 8-2 shows how this is done at the child constructor declaration.
The colon, ":", and keyword base call the base class constructor
with the matching parameter list. The first line of output shows the
base class constructor being called with the string "From
Derived".</td>
</tr>
<tr>
<td>Sometimes you may want to create your own implementation of a method
that exists in a base class. The Child class does this by declaring
it's own print() method. The Child print() method hides the Parent
print method. 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. This is done by prefixing the method name with
"base.". Using the "base" keyword, you can
access any of a base class' public or protected class members. 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.
This is done in the last statement of the Child class Main() method.
Remember that a derived class is a specialization of it's base class.
This fact allows us to perform a conversion on the derived class, making
it an instance of it's base class. 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.
You can control instantiation of your base class and call it's methods
either implicitly or explicitly. 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: 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 + -