public abstract class Animal
{
public string type = "**类";
public abstract void ShowType();
public void Eat()
{
Console.WriteLine("Animal都是会吃的.");
}
public string Color = "**色";
}
public class Bird : Animal
{
public string type = "Bird类";
public override void ShowType()
{
Console.WriteLine("当前的Type:{0}", type);
}
private string color;
public string Color
{
get { return color; }
set { color = value; }
}
}
public class Chicken : Bird
{
public string type = "Chicken类";
public override void ShowType()
{
//base.ShowType();
Console.WriteLine("当前的Type:{0}", type);
}
public void ShowColor()
{
Console.WriteLine("颜色:", Color);
}
}
class Program
{
static void Main(string[] args)
{
Bird b = new Bird();
b.Color = "白色";
Console.WriteLine("b Color:{0} ", b.Color);
b.ShowType();
Console.WriteLine("-".PadLeft(40, '-'));
Chicken c = new Chicken();
c.Color = "黑黑";
Console.WriteLine("c Color:{0} ", c.Color);
c.ShowType();
Console.WriteLine("-".PadLeft(40, '-'));
Console.ForegroundColor = ConsoleColor.Cyan;
Animal b2 = new Bird();
b2.Color = "白色2";
Console.WriteLine("b2 Color:{0} ", b2.Color);
Console.WriteLine("b2.Type:{0} ", b2.type);
b2.ShowType();
Console.WriteLine("-".PadLeft(40, '-'));
Animal c2 = new Chicken();
//c2.Color = "黑黑2"; //如果这样,会调用基类的颜色
Console.WriteLine("c2 Color:{0} ", c2.Color);
Console.WriteLine("c2.Type:{0} ", c2.type);
c2.ShowType(); //因为ShowType()方法被override重写了所以会调用子类的方法, 但属性和成员则会取自于父类的
Console.WriteLine("-".PadLeft(40, '-'));
Console.ReadLine();
}
} 运行结果:
posted on 2012-03-09 23:50
Victor.Stone 阅读(182)
评论(0) 编辑 收藏 引用 所属分类:
.net framework