真假Dcuk
namespace Duck { abstract class SubDuck { public abstract string Name { get; set; } //string name; //public string Name //{ // get { return name; } // set { name = value; } //} public void Swin() { Console.WriteLine("我会游泳。"); } public abstract void Jiao(); } class RealDuck : SubDuck, IFly { string name; public override void Jiao() { Console.WriteLine("嘎嘎~~~嘎嘎"); } public override string Name { get { return name; } set { name = value; } } public void ifiy() { Console.WriteLine("i can fly."); } } class RubberDuck : SubDuck, INolive { string name; public override void Jiao() { Console.WriteLine("唧唧~jiji唧唧"); } public override string Name { get { return name; } set { name = value; } } public void Nolive() { Console.WriteLine("i no lifeing."); } } class Program { static void Main(string[] args) { Random ri = new Random();//ding yi yi ge shuiji shu SubDuck[] sub = new SubDuck[100];//dingyi shuji shuzhu for (int i = 0; i < sub.Length; i++)//for xunhuan //foreach (var i in sub) ; { if (ri.Next(2) == 0)//shui ji de naqu liangge shu { sub[i] = new RealDuck(); sub[i].Name = "真鸭子" + (i + 1).ToString("000"); //sub[0].Swin = "游泳"; } else { sub[i] = new RubberDuck(); sub[i].Name = "橡皮鸭子" + (i + 1).ToString("000"); //sub[1].Swin = "游泳"; } } for (int i = 0; i < sub.Length; i++)//for out { Console.WriteLine("{0}", sub[i].Name); sub[i].Jiao(); IFly ff = sub[i] as IFly; if (ff != null) { ff.ifiy(); } else { ((INolive)sub[i]).Nolive(); } sub[i].Swin(); } //Console.WriteLine("{0} ", sub[1].Name, ); Console.ReadKey(); } }