using System;
using System.Data;public abstract class Animal
{
public abstract void ShowType(); public void Eat()
{
Console.WriteLine("Animal always eat.");
}
}public class Bird : Animal
{
private string type = "Bird"; public override void ShowType()
{
Console.WriteLine("Type is {0}", type);
} private string color; public string Color
{
get { return color; }
set { color = value; }
}
}public class Chicken : Bird
{
private string type = "Chicken";
public override void ShowType()
{
Console.WriteLine("Type is {0}", type);
}
public void ShowColor()
{
Console.WriteLine("Color is {0}", Color);
}
}public class TestInheritance
{
public static void Main()
{
Bird bird2 = new Chicken();
bird2.ShowType();
}
}
以上是一段关于C#继承的代码,运行的结果是Type is Chicken
但是添加监视发现bird2.type的值为Bird
想知道这是为什么,望高人指点,谢谢
解决方案 »
- 继承Control控件,实现在资源管理器可以编辑
- VS与SQl做的软件怎么安装到别的机子上
- 我的疑惑。关于sql防注入问题?
- 高手帮个忙啊。sql2000安装后 安装目录下没有MSSQL.1文件夹(在线等。。。。)
- 在C#里边groupBox1能够实现拖动吗
- 关于.net三层架构的问题
- 页面中DataGrid的刷新问题,请帮忙!
- 如何把一个FORM中的部分区域COPY下来,保存成一图片?
- vs.net的中文正式版出来了吗?IDE的版本号是多少?Framework的版本号是多少?
- WPF里面如何通过点击按钮,把另外一个窗体的BUTTON,显示到主窗体
- .net2 webbrowser 如何判断DocumentCompleted真正完成?
- 新手提问,在winform窗体中如果鼠标键盘无操作则关闭这个窗体?
实例化的是Chicken
Bird bird2 = new Chicken();
ShowType又是一个虚函数,调用谁的ShowType由对象实例的类型决定,而不是变量的类型决定
http://topic.csdn.net/u/20090116/11/960b32e0-4897-4ff0-ab9c-8de4335f7c1e.html