public void LetsMove() { for (int i = 0; i < animals.Count; i++) { if (animals[i] is IWalking) { IWalking iDog = animals[i] as IWalking; iDog.Walk(); } else if (animals[i] is ISwimming) { ISwimming iFish = animals[i] as ISwimming; iFish.Swim(); } else if (animals[i] is IFlying) { IFlying iBird = animals[i] as IFlying; iBird.Fly(); } else if (animals[i] is IFlying) { IFlying iButterfly = animals[i] as IFlying; iButterfly.Fly(); } } }
public void LetsMove() { if (animals is IWalking) { IWalking iDog = animals as IWalking; iDog.Walk(); } if (animals is ISwimming) { ISwimming iFish = animals as ISwimming; iFish.Swimm(); } if (animals is IFlying) { IFlying iButt = animals as IFlying; iButt.Fly(); } if (animals is IFlying) { IFlying iBird = animals as IFlying; iBird.Fly(); } }
static void Main(string[] args) { //Parent parent = new Parent();//Parent ref = Parent Object //parent.Sample();//Parent method //Parent parent = new Child();//Parent ref = Child Object //parent.Sample();//Child method //Parent child = new Child();//Parent ref = Child Object //child.Sample();//Child method //Animal animal = new Monkey(); //animal.Eat(); //animal.Breathe(); //animal.Look(); Bird bird = new Bird(); IFlying flyingObject = bird; flyingObject.TakeOff(); flyingObject.Fly(); flyingObject.Land(); Animal animal = bird; animal.Breathe(); Console.ReadKey(); }
public void LetsMove() { if (animals is IWalking) // проверяем, может ли собака ходить { IWalking iDog = animals as IWalking; // получаем интерфейсную ссылку. за счет условия выше мы можем быть уверены, что здесь не будет значения null iDog.Walk(); // вызываем метод интерфейса, он вызовет реализацию этого метода в классе Dog } if (animals is IFlying) // проверяем, может ли собака ходить { IFlying iFly = animals as IFlying; iFly.Fly(); } if (animals is ISwimming) { ISwimming iSwim = animals as ISwimming; iSwim.Swim(); } if (animals is IFlying) { IFlying iButterfly = animals as IFlying; iButterfly.Fly(); } }
public void ExecuteFly() { Flying.Fly(); }
void TreeLife(IFlying flying) { flying.TakeOff(); flying.Fly(); flying.Land(); }
public void Fly() { _fly.Fly(); }