Exemplo n.º 1
0
 public RubberDuck(IQuacking quacking, IFlying flying)
 {
     {
         Quacking = quacking;
         Flying   = flying;
     }
 }
Exemplo n.º 2
0
        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();
                }
            }
        }
Exemplo n.º 3
0
        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));
        }
Exemplo n.º 4
0
 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();
     }
 }
Exemplo n.º 5
0
        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();
        }
Exemplo n.º 6
0
 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();
     }
 }
Exemplo n.º 7
0
 public void SetFlyingInterface(IFlying flying)
 {
     Flying = flying;
 }
 public Rubber(ISwimming swim, IQuacking quack, IFlying fly)
 {
     swim_  = swim;
     quack_ = quack;
     fly_   = fly;
 }
Exemplo n.º 9
0
 public UniversalDuck(IFlying fly, IQuacking quack, ISwimming swim) : base(fly, quack, swim)
 {
 }
Exemplo n.º 10
0
 void TreeLife(IFlying flying)
 {
     flying.TakeOff();
     flying.Fly();
     flying.Land();
 }
Exemplo n.º 11
0
 protected BaseDuck(IFlying fly, IQuacking quack, ISwimming swim)
 {
     _fly   = fly ?? throw new ArgumentNullException();
     _swim  = swim ?? throw new ArgumentNullException();
     _quack = quack ?? throw new ArgumentNullException();
 }
Exemplo n.º 12
0
 public WildDuck(IFlying flying, IQuacking quacking)
 {
     Flying   = flying;
     quacking = Quacking;
 }
Exemplo n.º 13
0
 public Penguin(ISwimming swim, IQuacking quack, IFlying fly)
 {
     swim_  = swim;
     quack_ = quack;
     fly_   = fly;
 }
Exemplo n.º 14
0
        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();
            }
        }
Exemplo n.º 15
0
 public UniversalBird(IFlying flying, ISound sound, ISwimming swimming) : base(flying, sound, swimming)
 {
 }
Exemplo n.º 16
0
 public RubberDuck()
 {
     Quacking = new MuteQuacking();
     Flying   = new Flightless();
 }
Exemplo n.º 17
0
 public Mallarrd(ISwimming swim, IQuacking quack, IFlying fly)
 {
     swim_  = swim;
     quack_ = quack;
     fly_   = fly;
 }
Exemplo n.º 18
0
 protected BaseBird(IFlying flying, ISound sound, ISwimming swimming)
 {
     _fly   = flying ?? throw new ArgumentNullException();
     _sound = sound ?? throw new ArgumentNullException();
     _swim  = swimming ?? throw new ArgumentNullException();
 }
Exemplo n.º 19
0
    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);
    }