コード例 #1
0
ファイル: Program.cs プロジェクト: ysuinfsec/AnimalsInCSharp
        static void Main(string[] args)
        {
            Animal an1 = new Cat("Mrrrrrrro");

            Console.WriteLine(an1.Name);
            an1.Speak();
            Console.WriteLine("");


            Animal an2 = new Dog("Chalo");

            Console.WriteLine(an2.Name);
            an2.Speak();
            Console.WriteLine("");


            Animal an3 = new Bird("Kiwi");

            Console.WriteLine(an3.Name);
            an3.Speak();
            Console.WriteLine("");


            Animal an4 = new Lion("Catrin");

            Console.WriteLine(an4.Name);
            an4.Speak();
            Console.WriteLine("");
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: atssini23/AnimalsCsharp
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Animal brain     = new Animal("Brain");
            Animal asparagus = new Animal("Asparagus");

            //brain.Speak();
            //asparagus.Speak();

            Animal dog1 = new Dog("Michael");

            dog1.Speak();
            Dog newdog = (Dog)dog1;

            newdog.Speak();
            dog1.Move(7.5);
            System.Console.WriteLine(dog1.distance);
            // System.Console.WriteLine(dog1.Condition);

            Dog dog2 = new Dog("Hartley", 3, 2);

            dog2.Speak();
            dog2.Move(2.56);
            System.Console.WriteLine(dog2.distance);
            System.Console.WriteLine(dog2.Condition);
            //System.Console.WriteLine(dog2.legs);
            dog2.EatSnack();

            dog2.EatSnack();
        }
コード例 #3
0
        static void Main(string[] args)
        {
            Animal animal = new Animal();

            animal.Speak();
            animal.name = "Animal";

            Dog dog = new Dog(3);

            dog.Speak();
            dog.Name = "Dog";
            dog.Name = "New Dog";
            int numberOfSteps = dog.Run(4);

            bool isAWinner = false;

            do
            {
                int suma = 0;
                suma += numberOfSteps;
                if (suma >= 10)
                {
                    isAWinner = true;
                    Console.WriteLine("Dog is winner");
                }
                else
                {
                    Console.WriteLine("No terminó la carrera");
                }
            } while (isAWinner);

            Console.WriteLine("Dog name= " + dog.Name);
            //Console.WriteLine("Animal name= " + animal.name);

            Cat cat = new Cat(3);

            cat.Speak();
            cat.Name = "New Cat";
            int numberOfSteps2 = cat.Run(4);

            bool isAWinner2 = false;

            do
            {
                int suma2 = 0;
                suma2 += numberOfSteps;
                if (suma2 >= 10)
                {
                    isAWinner2 = true;
                    Console.WriteLine("Cat is winner");
                }
                else
                {
                    Console.WriteLine("No terminó la carrera");
                }
            } while (isAWinner2);


            //10
            Console.WriteLine("Cat name= " + cat.Name);
            // Console.WriteLine("Animal name= "+animal.name);

            Duck duck = new Animals.Duck(3);

            duck.Speak();
            duck.Name = "New Duck";
            int  numberOfSteps3 = duck.Run(4);
            bool isAWinner3     = false;

            do
            {
                int suma3 = 0;
                suma3 += numberOfSteps2;
                if (suma3 >= 10)
                {
                    isAWinner3 = true;
                    Console.WriteLine("Duck winner");
                }
                else
                {
                    Console.WriteLine("No terminó la carrera");
                }
            } while (isAWinner3);
            Console.WriteLine("Duck name= " + duck.Name);
            //Console.WriteLine("Animal name= " + animal.name);



            //Animal dog2 = new Dog();
            //dog2.Speak();


            //List<Animal> VetList = new List<Animal>();

            //VetList.Add(new Cat());
            //VetList.Add(new Dog());
            //VetList.Add(new Duck());

            //foreach (Animal animal in VetList)
            //{
            //    animal.Speak();
            //}
            Console.ReadLine();
        }
コード例 #4
0
        public static void Main()
        {
            // polymorphism - we use given data type through other data type (interface or parent class)
            IAnimal cat = new Cat("Dzhoni", 7);
            IAnimal dog = new Dog("Sharo", 5);

            Console.WriteLine(cat.Speak());
            Console.WriteLine(dog.Speak());

            // no polymorphism
            var secondCat = new Cat("Dzhinks", 3);
            Cat thirdCat  = new Cat("Fluffy", 1);

            Console.WriteLine(secondCat.Purr());
            Console.WriteLine(thirdCat.Purr());
            // cat has no Purr() method, because it's IAnimal

            var secondDog = new Dog("Doggy", 3);
            Dog thirdDog  = new Dog("Spike", 4);

            Console.WriteLine(secondDog.CatchBone());
            Console.WriteLine(thirdDog.CatchBone());
            // dog has no CatchBone() method, because it's IAnimal

            Console.WriteLine();

            // without polymorphism
            List <Cat> cats = new List <Cat>();

            cats.Add(secondCat);
            cats.Add(thirdCat);

            List <Dog> dogs = new List <Dog>();

            dogs.Add(secondDog);
            dogs.Add(thirdDog);

            foreach (var curCat in cats)
            {
                Console.WriteLine(curCat.Speak());
            }

            foreach (var curDog in dogs)
            {
                Console.WriteLine(curDog.Speak());
            }

            Console.WriteLine();

            // using polymorphism - mix different related types in the same collection
            List <IAnimal> animals = new List <IAnimal>();

            animals.Add(cat);
            animals.Add(secondCat);
            animals.Add(thirdCat);
            animals.Add(dog);
            animals.Add(secondDog);
            animals.Add(thirdDog);

            foreach (var curAnimal in animals)
            {
                Console.WriteLine(curAnimal.Speak());
            }

            Console.WriteLine();

            // using polymorphism - invoke abstract operations
            SayAnimalName(cat);
            SayAnimalName(secondCat);
            SayAnimalName(thirdCat);
            SayAnimalName(dog);
            SayAnimalName(secondDog);
            SayAnimalName(thirdDog);

            Console.WriteLine();

            // using polymorphism - declare a more generic field which will be initialized and "specialized" later
            IAnimal animal;
            string  animalType = "Cat";

            switch (animalType)
            {
            case "Cat": { animal = new Cat("Puh", 1); Console.WriteLine(animal.Name); break; }

            case "Dog": { animal = new Dog("Snoop", 1); Console.WriteLine(animal.Name); break; }

            default:
                break;
            }

            Console.WriteLine();

            // polymorphism
            Animal fourthCat = new Cat("Sam", 5);
            Animal fourthDog = new Dog("Chestar", 4);

            Console.WriteLine(fourthCat.ToString()); //Animals.Animals.Cat
            Console.WriteLine(fourthDog.ToString()); //I'm a dog.

            Console.WriteLine(fourthCat.Age);
            Console.WriteLine(fourthDog.Age);

            Kitten kitten = new Kitten("Baby", 1);

            Console.WriteLine(kitten.Speak());

            Console.WriteLine();

            try
            {
                Animal oldCat = new Cat("Oldi", 150);
            }
            catch (InvalidAnimalAgeException ex)    // defined exception class in folder Exceptions
            {
                Console.WriteLine(ex.MinAge);
                Console.WriteLine(ex.MaxAge);
            }
        }