public _4_02_Inheritance() { Notebook notebook = new Notebook(); notebook.Boot(); notebook.CloseLid(); Computer pc1 = notebook; // 암시적 형변환 pc1.Boot(); pc1.Shutdown(); // 부모 클래스인 Computer가 자식 클래스가 되는 것은 가능하나 // 자식 클래스가 부모 클래스로 되는 것은 불가능하다. Desktop desktop = new Desktop(); Netbook netbook = new Netbook(); DeviceManager deviceManager = new DeviceManager(); deviceManager.TurnOff(desktop); deviceManager.TurnOff(netbook); deviceManager.TurnOff(notebook); Computer[] machines = new Computer[] { new Notebook(), new Desktop(), new Netbook() }; foreach (Computer device in machines) { deviceManager.TurnOff(device); } }
static void Main(string[] args) { Notebook notebook = new Notebook(); Computer pc1 = notebook; //암시적 형변환 가능 pc1.Boot(); pc1.Shutdown(); Computer pc = new Computer(); //Notebook notebook = (Notebook)pc; //명시적 형변환, 컴파일은 가능 실행하면 오류 발생 notebook = new Notebook(); pc1 = notebook; //부모 타입으로 암시적 형변환 Notebook note2 = (Notebook)pc1; //다시 본래 타입으로 명시적 형변환 note2.CloseLid(); }
static void Main(string[] args) { Computer pc = new Computer(); Notebook notebook = pc as Notebook; if (notebook != null) { notebook.CloseLid(); } // int n = 5; // if((n as string) != null) int n = 5; if (n is string) { Console.WriteLine("n is string"); } }
static void Main(string[] args) { #region 155 // 암시적 형변환 //Computer[] machines = new Computer[] { new Notebook(), new Desktop(), new Netbook() }; //DeviceManager manager = new DeviceManager(); //foreach (Computer device in machines) //{ // manager.TurnOff(device); //} #endregion 155 Computer pc = new Computer(); Notebook notebook = pc as Notebook; if (notebook != null) // 코드대로라면 if 문 내부의 코드가 실행될 가능성은 없다. { notebook.CloseLid(); } }