회원 로그인
|
Property (프로퍼티) (with Unity)
Property (프로퍼티)
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); } }
댓글 0개
| 엮인글 0개
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 |