//duplicate signature
        //public static string PetStatus(string owner, Dog dog)
        //{
        //    return owner;
        //}
        static void Main(string[] args)
        {
            Dog sparky = new Dog()
            {
                Name = "Sparky", Age = 3
            };
            Cat alice = new Cat()
            {
                Name = "Alice", IsLazy = true
            };

            PetStatus("Paul", sparky);
            PetStatus(alice);

            //no problem -> inherited types
            sparky.Eat();
            alice.Eat();

            //thanks to runtime polymorphism the right methods are called
            Pet firstPet = new Dog()
            {
                Name = "Sparky", Age = 3
            };
            Pet secondPet = new Cat()
            {
                Name = "Alice", IsLazy = true
            };

            firstPet.Eat();
            secondPet.Eat();

            List <Pet> pets = new List <Pet>();

            pets.Add(sparky); //dog
            pets.Add(alice);  //cat;
            pets.Add(new Pet()
            {
                Name = "Barnie"
            });

            //thanks to runtime polymorphism the right methods are called
            foreach (Pet pet in pets)
            {
                pet.Eat();
            }


            Console.ReadLine();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            //1.definition
            Object o  = 123;
            Object o1 = new Dog();

            int    ii = 'A';       //자동 형변환 ; 큰 집합 = 작은 집합
            Animal a  = new Dog(); //상속한 부분(animal에 있는것)만 가능, dog의 있는 건 호풀 불가능하다.

            a.Eat();               //상속한 부분(animal에 있는것)만 사용 가능하다.
            // a.Bark();   //dog의 있는 건 호풀 불가능하다.
            //Animal c = new Cat();   //Animal type의 면수; polymarphism(다형성) ; 큰 집합(super class) = 작은 집합
            //Animal d = new Dog();

            Console.WriteLine();

            //2.array
            Object[] oa = new Object[2];               //모든게 들어간다
            //3.parameter
            List <Object> olist = new List <object>(); //모든게 들어간다

            Console.WriteLine();
            int[] ia = new int[2];
            ia[0] = 'A';    //int 배열엔 int.char도 들어간다   *long면 int,char도 들어간다--

            Animal[] aa = new Animal[2];
            aa[0] = new Dog();
            aa[1] = new Cat();
            for (int i = 0; i < aa.Length; i++)
            {
                aa[i].Eat();
                aa[i].Sleep();
            }

            Console.WriteLine();
            List <Animal> list = new List <Animal>();

            list.Add(new Dog());
            list.Add(new Cat());
            foreach (var item in list)
            {
                item.Eat();
                item.Sleep();
                if (item is Cat)
                {
                    ((Cat)item).Meow();
                }                                          //(item is Cat) ->true/false
                if (item is Dog)
                {
                    (item as Dog).Bark();
                }                                           //(item as Dog) -> object/null
            }
            //4.return


            //5.overriding
            Animal d = new Dog();

            d.Eat();
            d = new Cat();
            d.Eat();
        }