static void Main(string[] args) { Console.WriteLine("Hello World!"); var duck = new Duck(); var plane = new Airplane(); var helicopter = new Helicopter(); var flyingThings = new List <IFlyable> { duck, plane, helicopter }; foreach (var thing in flyingThings) { if (thing is ITakeOff takeOff) { takeOff.TakeOff("Bahamas"); } thing.Fly(); } }
static void Main(string[] args) { var duck = new Duck(); var plane = new Airplane(); var helicopter = new Helicopter(); var flyingThings = new List <IFlyable> { duck, plane, helicopter }; foreach (var thing in flyingThings) { // check to see if this thing implement this interface // if so add it to takeoff so that can be messed with if (thing is ITakeOff takeOff) { takeOff.TakeOff("Bahamas"); } thing.Fly(); } }
static void Main(string[] args) { Duck d = new Duck(); //Duck对象d可以使用3种方法: //1.自身定义的; //2.父类定义的 //3.接口定义的 d.Fly(); d.Cook(); d.Swim(); //将子类(Duck)对象赋给基类变量 Bird b = d; //现在只能使用基类定义的Fly()方法 b.Fly(); //将Duck对象赋给ISwin接口变量 ISwim s = d; //现在只能使用接口定义的Swim()方法 s.Swim(); //将Duck对象赋给另一个实现的接口IFood接口变量 IFood f = d; //现在只能使用接口定义的Cook()方法 f.Cook(); Console.ReadKey(); }