본문 바로가기

.NET/C# Basic

프로퍼티 예제

반응형
인터넷에서 긁어왔습니다 

using System;

class GAME
{
private string strTitle;
private string strGenre;

public string Title
{
get
{
Console.WriteLine("Title 프로퍼티의 get 호출");
return strTitle;
}
set
{
Console.WriteLine("Title 프로퍼티의 set 호출");
strTitle = value;
}
}

public string Genre
{
get
{
Console.WriteLine("Genre 프로퍼티의 get 호출");
return strGenre;
}
set
{
Console.WriteLine("Genre 프로퍼티의 set 호출");
strGenre = value;
}
}
}

class Program
{
public static void Main()
{
GAME g = new GAME();

g.Title = "사일런트 힐 4";
g.Genre = "호러 어드벤쳐";

Console.WriteLine("게임 타이틀 : {0}", g.Title);
Console.WriteLine("게임 장르   : {0}", g.Genre);
}
}

/*
 
 프로퍼티 GET / SET

 

공부한 내용을 실습하고 요약해둡니다 ~

GAME 클래스에는 strTitle, strGenre 라는 private 멤버변수가 있습니다.

외부에서(Main) 접근시에는

이 변수에 직접 접근하지 않고

프로퍼티를 사용하여 클래스 내부에서 접근하는 방법을 사용하고 있습니다.

 

get 은 값을 가져오는 것

set 은 값을 설정하는 것

 

어려울게 하나도 없죠 !
[출처] [C#] 프로퍼티 get/set 구현|작성자 미쉘린


 * /







using System;
using System.Collections.Generic;
using System.Text;

namespace SetPoint
{
    class Point
    {
        private int x,y;
        public int proX
        {
            get
            {
                return x;
            }
            set
            {
                x = value;
            }
        }
        public int proY
        {
            get
            {
                return y;
            }
            set
            {
                y = value;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Point pt = new Point();
            pt.proX = 150;
            pt.proY = 220;
            Console.WriteLine("{0} {1}",pt.proX,pt.proY);
        }
    }

'.NET > C# Basic' 카테고리의 다른 글

C# PPT 3  (0) 2008.10.02
C# PPT 2  (2) 2008.10.02
프로퍼티 관련소스  (0) 2008.10.01
프로퍼티 설명 get/set  (0) 2008.10.01
프로퍼티  (0) 2008.10.01
인스턴스  (0) 2008.10.01
클래스 멥버 관련소스  (0) 2008.10.01
클래스 맴버  (0) 2008.10.01