.Net Project/.Net 3.5 Sp1

49장 Class(Filed)

Magic_kit 2009. 8. 11. 13:24
반응형




using System;

namespace Field
{
    public class Car
    {
        //변수Variable Type
        public string name; //소문자로
        //상수Constant Type : static -> 정적접근
        public const int m_birth = 2010; //멤버변수의 의미
        //읽기전용필드 Read Only
        public static readonly string color = "Red"; //_언어바스코어->속성매치
             
    }
    public class Human
    {
        //이름을 저장할 공간 ? Field
        private string _Name;

        //이름을 외부에서 사용 : 속성(Property)
        public string Name
        {
            get {
                    return _Name;
                }
            set {
                    _Name = value;
                }
        }
    }
}
-----------------------------------------------------------
using System;
using Field; //네임스페이스 참조

public class 필드
{
    public static void Main()
    {
        //Field.Car car = new Field.Car();
        Car car = new Car();//Car클래스의 인스턴스 생성 의미
        car.name = "에쿠스"; //이렇게 하면 안된다 (X)
        // Car.color = "aa" Error
        // Car.m_birth = 2009; Error
        Human na = new Human(); //Human클래스의 인스턴스생성
        na.Name = "홍길동"; //설정(set)
        Console.WriteLine("이름 : {0}", na.Name); //사용(get)
    }
}

반응형