1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace cshapExTwo8 { class Program { static void Main(string[] args) { /* 상속 1) 상속형태 class 자식클래스이름 : 부모클래스이름 ex) class Child : Parent { } 2) base키워드 2-1) 사용방법1 자식클래스의 생성자 바로 우측에 : base(인자) 와 같이 사용하여 부모클래스의 생성자를 호출 ex) public Child(string cname) : base(cname) // base는 부모생성자 호출 { } 2-1) 사용방법2 자식클래스에서 base.메서드 또는 base.변수와 같이 부모의 메서드나 변수에 접근할 때 사용 ex) base.showInfo(); 3) base키워드 사용시 자식객체생성시 부모와 자식간의 생성자 종료자 호출순서 Parent생성자 Child생성자 Child종료자 Parent종료자 4) sealed 키워드 class앞에 붙는 키워드로 상속을 못하도록 하는 클래스를 만듬(봉인클래스) ex) sealed class Parent { } * */ Child child = new Child("child"); child.showInfo(); //Console.ReadKey(); } } // 기반클래스, 부모클래스 class Parent { protected string name; public Parent(string _name) { this.name = _name; Console.WriteLine($"Parent생성자 name:{this.name}"); } ~Parent() { Console.WriteLine($"Parent종료자 name:{this.name}"); } public void showInfo() { Console.WriteLine("Parent- showInfo"); } } // 파생클래스, 자식클래스 class Child : Parent { public Child(string cname) : base(cname) // base는 부모생성자 호출 { Console.WriteLine($"Child생성자 name:{this.name}"); } ~Child() { Console.WriteLine($"Child종료자 name:{this.name}"); base.showInfo(); } } } | cs |
-----------------------------------------------결과창------------------------------
Parent생성자 name:child
Child생성자 name:child
Parent- showInfo
Child종료자 name:child
Parent- showInfo
Parent종료자 name:child
계속하려면 아무 키나 누르십시오 . . .
'프로그래밍' 카테고리의 다른 글
2_8. is연산자 와 as 연산자 (0) | 2019.01.21 |
---|---|
2_7.상속관계에서 클래스간의 형변환 (0) | 2019.01.21 |
2_5. 접근 제한자/한정자 (0) | 2019.01.18 |
2_4.this() (0) | 2019.01.18 |
2_3. 얕은복사와 깊은복사 (0) | 2019.01.16 |
댓글