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 void ShowColor()
{
Console.WriteLine("color is {0}.", Color);
}
}
class Program
{
static void Main(string[] args)
{
Chicken chicken = new Chicken();
chicken.ShowType(); Console.ReadKey();
}
}
请问输出的是什么???
麻烦说说原因????
Type is bird.但是,这只能说你程序写的烂。
貌似不能覆盖字段的
实际输出为Chicken类的基类Bird类ShowType()方法.public override void ShowType()
{
base.ShowType();
}如果要输出Type is chicken。using System;
public abstract class Animal
{
public abstract void ShowType();
public void Eat()
{
Console.WriteLine("Animal always eat.");
}
}
public class Bird : Animal
{
protected 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
{
public Chicken()
{
this.type = "chicken";
} public void ShowColor()
{
Console.WriteLine("color is {0}.", Color);
}
}
class Program
{
static void Main(string[] args)
{
Chicken chicken = new Chicken();
chicken.ShowType(); Console.ReadKey();
}
}
chicken.ShowType();中的chicken没有实现ShowType方法,调用Bird方法重写的ShowType方法。
而Chicken类继承了Bird类的ShowType一个是重写,一个是继承
http://topic.csdn.net/u/20110427/21/8f5630b5-da0d-42e6-9d1c-4a3257eb30e2.html
讲的是集成方面的(后面是讨论继承还是聚合类的问题),原本代码中chicken类是重写了ShowType()方法的
是我好奇,于是将chicken类中的ShowTyup()方法去掉然后把题目拿过来!!!看你这语气我想也没有必要说什么了。。我只是菜鸟一只,或许你根本就不屑我的回答。。不过没关系,我只是朝我认为正确的方向走!!!