コード例 #1
0
ファイル: Program.cs プロジェクト: smilesand/DesignPatterns
        /// <summary>
        /// 继承父类中的方法
        /// </summary>
        public static void Folder1()
        {
            Bird sparrow = new Sparrow();

            sparrow.Name = "麻雀";
            Console.WriteLine($"{sparrow.GetName()}可以飞行{sparrow.Fly(10)}公里");
            Bird wildGoose = new WildGoose();

            wildGoose.Name = "大雁";
            Console.WriteLine($"{wildGoose.GetName()}可以飞行{wildGoose.Fly(10)}公里");
        }
コード例 #2
0
        public static void Perform()
        {
            IBird    _sparrow = new Sparrow();
            IToyDuck _toyDuck = new PlasticToyDuck();
            // Wrap a bird in a birdAdapter so that it
            // behaves like toy duck
            IToyDuck _birdAdapter = new BirdAdapter(_sparrow);

            Console.WriteLine("Sparrow...");
            _sparrow.Fly();
            _sparrow.MakeSound();
            Console.WriteLine("Toy Duck");
            _toyDuck.Squeak();
            // bird behaving like a toy duck
            Console.WriteLine("BirdAdapter...");
            _birdAdapter.Squeak();
            Console.WriteLine();
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: everyloop/DotNetDemo
        static void Main(string[] args)
        {
            Parrot parrot1 = new Parrot(34);

            parrot1.Eat();
            parrot1.Fly();
            parrot1.Talk("Hello there!");
            parrot1.Talk();

            Console.WriteLine();

            Sparrow sparrow1 = new Sparrow()
            {
                Name = "Tom", Weight = 1.5
            };

            sparrow1.Eat();
            sparrow1.Fly();
            sparrow1.ClimbThroughSmallHole();

            Parrot parrot2 = new Parrot(15)
            {
                Name = "Pelle", Weight = 45.6
            };;
            Parrot parrot3 = new Parrot(15)
            {
                Name = "Anna", Weight = 235.6
            };;
            Sparrow sparrow2 = new Sparrow()
            {
                Name = "Mats", Weight = 23.4
            };;

            Bird[] birds = { parrot1, parrot2, sparrow1, parrot3, sparrow2 };

            Console.WriteLine();
            Console.WriteLine("Bird array:");
            Console.WriteLine();

            foreach (Bird bird in birds)
            {
                bird.Eat();
                if (bird is Parrot)
                {
                    Parrot temporaryParrot = (Parrot)bird;

                    //(bird as Parrot).Talk();
                    temporaryParrot.Talk();
                    temporaryParrot.Fly();
                }
                else
                {
                    Console.WriteLine($"{bird.Name} can not talk!");
                }
            }

            Console.WriteLine();

            foreach (Bird bird in birds)
            {
                bird.Die();
            }
        }