==> 생성자.cs


using System;

public class 생성자
{
    public static void Main(string[] args)
    {
        Car car = new Car(); // Car() 생성자를 사용해서 Car 클래스의 car 인스턴스 생성
        car.Print(); // 홍길동    // Car 클래스의 Print()메서드 사용.

        Car sonata = new Car("소나타");
        sonata.Print(); // 소나타

        Car santafe = new Car("산타페");
        santafe.Print(); // 산타페

        Car.Show(); // 정적생성자호출

    }
}


==============================================================================================


==> Car.cs


using System;
//[1] Class
public class Car
{
    //[2] Field
    private string name; // 이름

    //[3] Constructor : 생성자. 클래스 이름과 동일한 이름의 메서드
    public Car() // 기본 생성자(매개변수 없음)
    {
        name = "홍길동"; // 초기화
    }
    /// <summary>
    /// 자동차 이름을 넘기세요.
    /// </summary>
    /// <param name="name">자동차명</param>
    public Car(string name) // 매개변수 있는 생성자
    {
        this.name = name; // this.name은 name필드
    }

    static Car()
    // 정적멤버 초기화 생성자. LIFO형태. 매개변수 가질 수 없음
    // 나중에 선언된 객체 먼저 실행. 객체(인스턴스) 만들어 지기 전에 실행.
    {
        Console.WriteLine("언제?"); // static member 접근 할 때
    }

    //[4] Method
    public void Print()
    {
        Console.WriteLine("이름 : {0}", name);
    }

    public static void Show()
    {
        Console.WriteLine("정적");
    }
}

'.NET프로그래밍 > C# 3.5 SP1' 카테고리의 다른 글

47. 메서드(Method)  (0) 2009.08.12
46. 소멸자(Destructor)  (0) 2009.08.11
44. 필드(Field)  (0) 2009.08.11
43. 클래스(Class)  (0) 2009.08.11
42. 컬렉션 - 5. 리스트(List)  (0) 2009.08.10
Posted by holland14
: