반응형
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 | using System; using System.Collections.Generic; using System.Text; namespace 생성자 { class Person { private string name; public Person() { Console.WriteLine( "Person()" ); } public Person(string name) { Console.WriteLine(name); } } class BitStudent : Person { // C# 생성자 : 객체 초기화하는 함수 !! // 1) 인스턴스 생성자 : 객체가 만들어질때 불려지는 생성자 !! public static string InstructorName; public BitStudent() { Console.WriteLine( "BitStudent()" ); } //2) 정적 생성자 : static 생성자 !! // 클래스가 메모리에 로딩될때 호출되어지는 생성자 !! // --> 무조건 1번만 호출이 된다 !! static BitStudent() { // 스태틱 멤버에 대한 초기화 !! InstructorName = "Jang" ; } // base: C#에서 부모 클래스를 의미한다 !! // this() : 자기 클래스의 다른 생성자를 의미한다 !! public BitStudent(string name) : this () { // this.name = name; Console.WriteLine(name); } } class Program { static void Main(string[] args) { // BitStudent bit = new BitStudent("홍길동"); Console.WriteLine(BitStudent.InstructorName); } } } |