static void Main(string[] args) { Mammal mammal = new Mammal(); mammal.Name = "포유류"; mammal.Nurse(); Dog ppoppi = new Dog(); ppoppi.Name = "뽀삐"; ppoppi.Nurse(); ppoppi.Bark(); Cat chichi = new Cat(); chichi.Name = "치치"; chichi.Meow(); if (ppoppi is Mammal) //true { Console.WriteLine("형변환을 할 수 있습니다"); Mammal mammal1 = ppoppi as Mammal; //참조형식은 as를 써서 형변환 mammal1.Nurse(); //mammal1.Bark(); //안됨 } }
static void Main(string[] args) { Mammal mammal = new Mammal(); mammal.Name = "포유류"; mammal.Nurse(); Dog ppoppi = null; // new Dog(); /*ppoppi.Name = "뽀삐"; * ppoppi.Nurse(); * ppoppi.Bark();*/ /*Cat chichi = new Cat(); * chichi.Name = "치치"; * chichi.Nurse(); * chichi.Meow();*/ if (ppoppi is Mammal) // 뽀삐는 포유류인가???? { // 값형식은 long = int값; Mammal mammal1 = ppoppi; // 참조형식은 as를 써서 형변환 mammal1.Nurse(); // mammal1.Bark(); // error } if (mammal is Dog) { ppoppi = mammal as Dog; ppoppi.Nurse(); ppoppi.Bark(); } }