public RubberDuck(IQuacking quacking, IFlying flying) { { Quacking = quacking; Flying = flying; } }
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 IDuck Create(FlyType fly, QuackType quack, SwimType swim) { IFlying flying = _fly.Create(fly); IQuacking quacking = _quack.Create(quack); ISwimming swimming = _swim.Create(swim); return(new UniversalDuck(flying, quacking, swimming)); }
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(); } }
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; 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(); } }
public void SetFlyingInterface(IFlying flying) { Flying = flying; }
public Rubber(ISwimming swim, IQuacking quack, IFlying fly) { swim_ = swim; quack_ = quack; fly_ = fly; }
public UniversalDuck(IFlying fly, IQuacking quack, ISwimming swim) : base(fly, quack, swim) { }
void TreeLife(IFlying flying) { flying.TakeOff(); flying.Fly(); flying.Land(); }
protected BaseDuck(IFlying fly, IQuacking quack, ISwimming swim) { _fly = fly ?? throw new ArgumentNullException(); _swim = swim ?? throw new ArgumentNullException(); _quack = quack ?? throw new ArgumentNullException(); }
public WildDuck(IFlying flying, IQuacking quacking) { Flying = flying; quacking = Quacking; }
public Penguin(ISwimming swim, IQuacking quack, IFlying fly) { swim_ = swim; quack_ = quack; fly_ = fly; }
static void Main(string[] args) { Aircraft Mig = new Aircraft(); // обьект класса самолет с интерфейсом полета (IFly) Mig.Speed = 1000; Mig.Fly(); Mig.Print(); Console.WriteLine(); Ship Liner = new Ship(); Liner.SpeedSwim = 30; Liner.Swim(); Liner.Print(); Console.WriteLine(); Amphibian An = new Amphibian(); //обьект класса самолет с интерфейсом плавания (ISwim) и родительского класса смолет с (IFly) An.Speed = 500; An.SpeedSwim = 90; An.Swim(); An.Print(); //коллизия имен - решение через склеивание Console.WriteLine(); ((ISwiming)An).Capacity(); //коллизия имен - решение через кастинг ((IFlying)An).Capacity(); //кастинг Console.WriteLine(); An.SwimingCapacity(); //кастинг + обертывание (Переименование) Console.WriteLine(); IFlying[] objectsArray = new IFlying[4] { new Aircraft("aircraft1", 10), new Amphibian("amphibian1"), new Aircraft("aircraft2", 11), new Amphibian("amphibian2") }; //создание массива типа Iterface1 (IFlying) foreach (IFlying flying in objectsArray) //вызов метода Swim из Interface2 для всех элементов массива котороые его поддерживают { if (flying is ISwiming) { ((ISwiming)flying).Swim(); } } Console.WriteLine(); Console.WriteLine("*************************"); Console.WriteLine("* Before sort *"); Console.WriteLine("*************************"); foreach (IFlying flying in objectsArray) { ((Aircraft)flying).Print(); } Array.Sort(objectsArray); //сортировка массива Console.WriteLine("*************************"); Console.WriteLine("* After sort *"); Console.WriteLine("*************************"); foreach (IFlying flying in objectsArray) { ((Aircraft)flying).Print(); } Console.WriteLine(); Aircraft[] planes = new Aircraft[5] { new Aircraft("mig-25", 15), new Aircraft("mig-21", 12), new Aircraft("su-27", 19), new Aircraft("su-24", 18), new Aircraft("F-35", 25) }; Console.WriteLine("*************************"); Console.WriteLine("* Before sort *"); Console.WriteLine("*************************"); foreach (Aircraft model in planes) { model.Print1(); } Array.Sort(planes, new CompAircraftName()); //сортировка массива по имени Console.WriteLine("*************************"); Console.WriteLine("* After sort by name *"); Console.WriteLine("*************************"); foreach (Aircraft model in planes) { model.Print1(); } Array.Sort(planes, new CompareAircraftWeight()); //сортировка массива по весу Console.WriteLine("*************************"); Console.WriteLine("* After sort by weight *"); Console.WriteLine("*************************"); foreach (Aircraft model in planes) { model.Print1(); } }
public UniversalBird(IFlying flying, ISound sound, ISwimming swimming) : base(flying, sound, swimming) { }
public RubberDuck() { Quacking = new MuteQuacking(); Flying = new Flightless(); }
public Mallarrd(ISwimming swim, IQuacking quack, IFlying fly) { swim_ = swim; quack_ = quack; fly_ = fly; }
protected BaseBird(IFlying flying, ISound sound, ISwimming swimming) { _fly = flying ?? throw new ArgumentNullException(); _sound = sound ?? throw new ArgumentNullException(); _swim = swimming ?? throw new ArgumentNullException(); }
static void Main() { Animal[] animals = new Animal[4]; Animal animal = new Animal(); Bird bird = new Bird(); bird.name = "Vasia"; Cat cat = new Cat(); Lion lion = new Lion(); animals [0] = animal; animals [1] = bird; animals [2] = cat; animals [3] = lion; animal.Eat(); bird.Eat(); cat.Eat(); lion.Eat(); //animal.cells = 0; Console.WriteLine(); int i; for (i = 0; i < 4; i++) { animals[i].Eat(); } Console.WriteLine(); ((Cat)animals[2]).Eat(); Console.WriteLine(); IFlying[] flying = new IFlying[2]; Bat bat = new Bat(); flying [0] = bat; flying [1] = bird; for (i = 0; i < 2; i++) { flying [i].Fly(); } Console.WriteLine(); Man man = new Man(); Batman batman = new Batman(); man.Eat(); batman.Eat(); batman.Fly(); Console.WriteLine(); Console.WriteLine(Animal.Quantity); }