Menu



Manage

Study_C# > 9/Program.cs Lines 65 | 1.7 KB
다운로드

                        using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace week10
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Dog dog = new Dog("개", 1);
            dog.AddAge();
            dog.Bark();
            //dog.Meow();
            //dog.Name = "개";
            //dog.Age = 1;

            Cat cat = new Cat("냥", 10);
            //cat.Name = "냥";
            //cat.Age = 2;
            cat.AddAge();
            //cat.Bark();
            cat.Meow();

            Animal animal = new Animal("뱀",2);
            animal.AddAge();
            //animal.Bark();
            //animal.Meow();

            //Console.WriteLine(dog.Age);
            //Console.WriteLine(cat.Age);

            List<Dog> dList = new List<Dog>();
            List<Cat> cList = new List<Cat>();
            dList.Add(dog);
            //dList.Add(cat);
            //dList.Add(animal);

            cList.Add(cat);
            //cList.Add(dog);
            //cList.Add(animal);

            List<Animal> aList = new List<Animal>();        //다형성 예
            aList.Add(dog);
            aList.Add(cat);
            aList.Add(animal);

            foreach(var a in aList){
                a.AddAge();
                //is 연산자를 써서 안정적으로 쓰는 법 
                if(a is Dog)
                    ((Dog)a).Bark();
                //걍 한방에 처리하는 법
                Cat c = a as Cat;   //TryParse와 유사함
                if (c != null)
                    c.Meow();
                    //a.Meow()
            }
        }
    }
}