.NET프로그래밍/C# 3.5 SP1

44. 필드(Field)

holland14 2009. 8. 11. 11:32

==> 필드.cs


using System;
using Field; // 네임스페이스 참조

public class 필드
{
    public static void Main(string[] args)
    {
        // Field.Car car = new Field.Car(); // 인스턴스 생성
        // 위의 코드 간단히 하기 위해 미리 네임스페이스 위에 선언
        Car car = new Car(); // Car클래스의 인스턴스 생성
        car.name = "에쿠스"; // 이렇게 하면 X
        //Car._Color = "aa"; // 에러...
        //Car.m_birth = 2009; // 에러

        Human na = new Human(); // Human 클래스의 인스턴스(객체) 생성
        na.Name = "홍길동"; // 설정(set)
        Console.WriteLine("이름 : {0}", na.Name); // 사용(get -> 읽기)
    }
}


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

==> Car.cs


using System;

namespace Field
{
    public class Car
    {  
        // 필드(=멤버변수) 3개
        // 변수(Variable)
        public string name; // 소문자로

        // 상수(Constant) : 정적 접근. 읽기 전용 멤버
        public const int m_birth = 2010; // m_ : 멤버변수(member variable)

        // 읽기전용 필드(ReadOnly)
        public static readonly string _Color = "Red"; // _언더스코어 -> 속성매치
    }

    public class Human
    {
        // 이름 저장 공간? 필드
        private string _Name; // 접근 한정자

        // 이름을 외부에서 사용 :  속성(Property) : 속성 선언 정의 메서드
        public string Name {
            get { return _Name; } // 읽기
            set { _Name = value; } // 쓰기
        }
       
    }
}