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(); }
void Act(GameObject animal) { switch (input) { case InputType.LMB: { ISpeak speak = animal.GetComponent <ISpeak>(); if (speak != null) { speak.Speak(); speakCount++; } break; } case InputType.RMB: { IWalk walk = animal.GetComponent <IWalk>(); if (walk != null) { walk.Walk(); walkCount++; } break; } case InputType.Space: { ISwim swim = animal.GetComponent <ISwim>(); if (swim != null) { swim.Swim(); swimCount++; } break; } case InputType.I: { IID id = animal.GetComponent <IID>(); if (id != null) { id.Identify(); } break; } default: break; } }
static void Main(string[] args) { Duck duck = new Duck(); duck.Fly(); duck.Swim(); duck.Cook(); Bird bird = duck; bird.Fly(); //将Duck对象赋给ISwin接口变量 ISwim s = duck; //只会游泳 s.Swim(); //将Duck对象赋给另一个实现的接口IFood接口变量 IFood food = duck; //只会Cook food.Cook(); Console.ReadKey(); }
public void DoSwim() { swimAction.Swim(); }
public void Swim(ISwim swim) { Console.WriteLine("Person lives " + this.GetType().Name + " swim in " + swim.GetType().Name); swim.Swim(); }
public void Welcome(ISwim obj) => obj.Swim();
public void MakeSwim() { _swim.Swim(); }
public void Swim() { _swim.Swim(); }
public virtual void SPropFunc() { _swim.Swim(); }
public virtual void Swim() { _swim?.Swim(); }
public virtual void MakeSwim() { _swim.Swim(); }
public void Swim() { Console.WriteLine("Please tell me that you know how to swim!"); _swimBehaviour.Swim(); Console.WriteLine(); }
public void Swim() { iswim.Swim(); }