📄 base class with a virtual method.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>Base Class With a Virtual Method</title>
</head>
<body background="../basic3.gif">
<table border="0" cellspacing="0" width="596">
<tr>
<td>
<p align="center"><font color="#FF0000" size="4"><b>Base Class With a
Virtual Method</b></font></td>
</tr>
<tr>
<td></td>
</tr>
<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">
DrawingObject<br>
{<br>
</font><font color="#0000ff" size="2"> </font><font size="2">
</font><font color="#0000ff" size="2">public</font><font size="2"> </font><font color="#0000ff" size="2">virtual</font><font size="2">
</font><font color="#0000ff" size="2">void</font><font size="2"> Draw()<br>
{<br>
Console.WriteLine("I'm
just a generic drawing object.");<br>
}<br>
}</font></td>
</tr>
<tr>
<td>Listing 9-1 shows the DrawingObject class. This will be the base
class for other objects to inherit from. It has a single method
named Draw(). The Draw() method has a virtual modifier. The
virtual modifier indicate to derived classes that they can override this
method. The Draw() method of the DrawingObject class performs a
single action of printing the statement, "I'm just a generic drawing
object.", to the console.</td>
</tr>
<tr>
<td>
<h5>Listing 9-2. Derived Classes With Override Methods:
Line.cs, Circle.cs, and Square.cs</h5>
</td>
</tr>
<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">
Line : DrawingObject<br>
{<br>
</font><font color="#0000ff" size="2">public</font><font size="2"> </font><font color="#0000ff" size="2">override</font><font size="2">
</font><font color="#0000ff" size="2">void</font><font size="2"> Draw()<br>
{<br>
Console.WriteLine("I'm a
Line.");<br>
}<br>
}<br>
<font color="#0000ff" size="2"><br>
public</font> <font color="#0000ff" size="2">class</font> Circle :
DrawingObject<br>
{<br>
<font color="#0000ff" size="2"> </font> <font color="#0000ff" size="2">public</font>
<font color="#0000ff" size="2">override</font> <font color="#0000ff" size="2">void</font>
Draw()<br>
{<br>
Console.WriteLine("I'm a
Circle.");<br>
}<br>
}<br>
<font color="#0000ff" size="2"><br>
public</font> <font color="#0000ff" size="2">class</font> Square :
DrawingObject<br>
{<br>
<font color="#0000ff" size="2"> </font> <font color="#0000ff" size="2">public</font>
<font color="#0000ff" size="2">override</font> <font color="#0000ff" size="2">void</font>
Draw()<br>
{<br>
Console.WriteLine("I'm a
Square.");<br>
}<br>
}</font></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -