회원 로그인
정보기억 정보기억에 체크할 경우 다음접속시 아이디와 패스워드를 입력하지 않으셔도 됩니다.
그러나, 개인PC가 아닐 경우 타인이 로그인할 수 있습니다.
PC를 여러사람이 사용하는 공공장소에서는 체크하지 마세요.
소셜네트워크 서비스를 통해서 로그인하시면 별도의 로그인 절차없이 회원서비스를 이용하실 수 있습니다.


최근 게시물

1.노션에서 작성 중

1.노션에서 작성 중

개편하기 전까지 노션에서 작성 중

2024.04.04//read more

2.ChatGPT

2.ChatGPT

OpenAI로 대규모 언어 모델대화형...

2023.03.16//read more

3.노코딩 게임 엔진 - 빌..

3.노코딩 게임 엔진 - 빌..

빌드 지원안드로이드iOS윈도우즈특이사...

2023.03.14//read more

4.(완료) 미접속 회원 정..

4.(완료) 미접속 회원 정..

[완료] 36명의 회원을 정리하였습니...

2023.02.16//read more

5.매뉴얼 플러스 - 전자제..



안정적인 DNS 서비스 DNSEver
DNS Powered by DNSEver.com


Property (프로퍼티) (with Unity)

푸딩뱃살 | 2015.11.18 10:50 | 조회 2443
Property (프로퍼티)

Property 사용 전
using UnityEngine;
using System.Collections;

public class CMonster
{
    private string _name;
    private int hp;

    public void SetName(string name) { _name = name; }

    public string GetName() { return _name; }

    public void SetHp(int hp)
    {
        // 범위에 대한 예외 처리
        if (hp <= 0) hp = 0;
        this.hp = hp;
    }

    public int GetHp() { return hp; }
}


public class CPropertyStudy : MonoBehaviour {

    void Start () {
        CMonster monster = new CMonster();
        monster.SetName("오우거");
        monster.SetHp(-100);

        Debug.Log("몬스터 이름 : " + monster.GetName());
        Debug.Log("몬스터 체력 : " + monster.GetHp());
    }
}
Property 사용 후
using UnityEngine;
using System.Collections;

// Property 사용 후
// 데이터형 클래스에 활용

public class CMonster
{
    private string _name;
    private int hp;

    public string Name
    {
        get { return this.name; }  // get{ } // Name() 함수를 호출
        set { this.name = value; }  // set{ } // Name(인수 하나)로 메모리에 하나만 저장

    public int Hp
    {
        get { return this.hp; }
        set {
            if (value <= 0) value = 0;
            this.hp = value;
        }
    }
}

public class CPropertyStudy : MonoBehaviour
{
    private int _gameLevel;
    public void SetGameLevel(int level)
    {
        _gameLevel = level;
    }

    public int GetGameLevel()
    {
        return _gameLevel;
    }
    
    //--------------------------------------

    public int GameLevel
    {
        set { _gameLevel = value; }
        get { return this._gameLevel; }
    }

    void Start()
    {
        // SetGameLevel(1);
        GameLevel = 1;

        // Debug.Log("게임 레벨 : " + GetGameLevel());
        Debug.Log("게임 레벨 : " + GameLevel);

        /*
        CMonster monster = new CMonster();
        monster.Name = "오우거";
        monster.Hp = 100;

        Debug.Log("몬스터 이름 : " + monster.GetName());
        Debug.Log("몬스터 체력 : " + monster.GetHp());
        */
    }
}
virtual, abstract 프로퍼티 사용법
using UnityEngine;
using System.Collections;

// 추상 클래스
public abstract class CMonster
{
    protected string name;
    protected int hp;

    // 가상 프로퍼티
    // - 자식은 부모의 프로퍼티를 필요에 의해 재정의 해서 쓸 수 있음
    public virtual string Name
    {
        // set, get, value
        set { this.name = value; }  // value는 매개변수, 인수값이 무조건 value로
                                    // 타입은 자동으로 맞춰짐
        get { return this.name; }
    }

    // 추상 프로퍼티
    // - 자식은 프로퍼티를 재정의 해라
    public abstract int Hp
    {
        get;
        set;
    }
}

public class Orc : CMonster
{
    // Name 프로퍼티의 set 프로퍼티를 재정의함 (강제성 x)
    public override string Name
    {
        set
        {
            if (value.Length <= 0) value = "홍길동";
            name = value;
        }
    }

    // Name 프로퍼티의 set 프로퍼티를 재정의함 (강제성 o)
    public override int Hp
    {
        get { return hp; }
        set
        {
            if (value <= 0) value = 0;
            hp = value;
        }
    }
}

public class CPropertyStudy : MonoBehaviour
{

    public int value = 10;

    void Start()
    {

        Debug.Log("value : " + value);

        CMonster monster = new Orc();  // 업케스팅

        monster.Name = "";
        monster.Hp = -100;

        Debug.Log("몬스터 이름 : " + monster.Name);
        Debug.Log("몬스터 체력 : " + monster.Hp);
    }
}
285개(6/15페이지)
프로그래밍
번호 제목 글쓴이 조회 날짜
185 [PHP] 회원가입 + 로그인 스크립트 (with Unity) [2+1] 푸딩뱃살 12240 2015.12.06 17:31
184 [C#] C# 추천 서적 푸딩뱃살 1321 2015.12.06 17:16
183 [C/C++] C++ 참고 사이트 푸딩뱃살 1182 2015.12.04 16:13
182 [PHP] 클래스 푸딩뱃살 3581 2015.12.04 14:40
181 [PHP] CodeIgniter(코드이그나이트) 첨부파일 푸딩뱃살 2932 2015.12.04 14:40
180 [C#] Delegate (델리게이트) (with Unity) 푸딩뱃살 6267 2015.12.01 10:44
179 [PHP] php 함수 푸딩뱃살 2219 2015.11.30 15:33
178 [PHP] Dictionary (딕셔너리) 첨부파일 푸딩뱃살 2869 2015.11.27 12:37
177 [PHP] 배열 푸딩뱃살 2170 2015.11.27 12:37
176 [PHP] 변수 선언 / 산술 연산 푸딩뱃살 2183 2015.11.27 11:14
175 [C#] Dictionary (딕셔너리) (with Unity) 첨부파일 [3+3] 푸딩뱃살 14483 2015.11.25 10:29
174 [C#] List (리스트) (with Unity) 첨부파일 푸딩뱃살 12088 2015.11.24 10:22
173 [C#] Generic (제네릭) (with Unity) 푸딩뱃살 4889 2015.11.22 12:32
172 [C/C++] Templete (템플릿) 푸딩뱃살 2046 2015.11.22 12:15
171 [C#] 키보드 입력 푸딩뱃살 3570 2015.11.21 18:00
170 [C#] 예외와 예외 처리 푸딩뱃살 4124 2015.11.21 17:39
169 [C#] Struct (구조체) (with Unity) 푸딩뱃살 5124 2015.11.18 22:08
>> [C#] Property (프로퍼티) (with Unity) 푸딩뱃살 2444 2015.11.18 10:50
167 [C#] interface (인터페이스) (with Unity) 첨부파일 푸딩뱃살 3308 2015.11.15 17:32
166 [C#] 스트림 (stream) - 문자, 바이너리 읽기/쓰기 푸딩뱃살 12780 2015.11.14 18:24