회원 로그인
|
Generic (제네릭) (with Unity)
C# Generic (제네릭)
제네릭 타입 클래스를 정의하고 자료형을 맘대로 캐스팅할 수 있다.
예로 일반 적인 정수형 덧셈 클래스에서 정수형 매계 변수를 2개를 받아 덧셈을 수행해서 정수형으로 리턴한다. 정수형으로 정의된 클래스에선 다른 타입으로 실행할 수 없다.
이것을 제네릭으로 사용하여 다른 자료형으로 실행할 수 있게 된다.
C++에서는 Templete (템플릿) 사용
C#에선 Gerneric (제네릭) 사용
제네릭 클래스 예제
*유니티에서 Main Camera에 컴포넌트 추가하여 실행
제네릭 타입 클래스를 정의하고 자료형을 맘대로 캐스팅할 수 있다.
예로 일반 적인 정수형 덧셈 클래스에서 정수형 매계 변수를 2개를 받아 덧셈을 수행해서 정수형으로 리턴한다. 정수형으로 정의된 클래스에선 다른 타입으로 실행할 수 없다.
이것을 제네릭으로 사용하여 다른 자료형으로 실행할 수 있게 된다.
C++에서는 Templete (템플릿) 사용
C#에선 Gerneric (제네릭) 사용
제네릭 클래스 예제
*유니티에서 Main Camera에 컴포넌트 추가하여 실행
using UnityEngine; using System.Collections; public class Monster { private string _name; private int _damage; public Monster() { _name = "오크"; _damage = 10; } public void Attack() { Debug.Log(_name + " 몬스터가 " + _damage + "로 공격을 합니다."); } } public class Weapon { private int _str; private int _dex; private int _con; public Weapon() { _str = 80; _dex = 30; _con = 50; } public void ShowStat() { Debug.Log("힘 : " + _str + ", 민첩" + _dex + ", 체력 : " + _con); } } // [제네릭 클래스] // public class 클리스명<T> public class GenericContainer<T> { private T _value; public void SetValue(T value) { _value = value; } public T GetValue() { return _value; } } public class GenericEx1 : MonoBehaviour { // Use this for initialization void Start() { GenericContainer<Monster> gContain1 = new GenericContainer<Monster>(); // Monster monster = new Monster(); // 일반 // gContain1.SetValue(monster); // 일반 gContain1.SetValue(new Monster()); // 제네릭 //Monster monster = gContain1.GetValue(); // 일반 //monster.Attack(); // 일반 gContain1.GetValue().Attack(); // 제네릭 GenericContainer<Weapon> gContain2 = new GenericContainer<Weapon>(); gContain2.SetValue(new Weapon()); gContain2.GetValue().ShowStat(); GenericContainer<string> gContain3 = new GenericContainer<string>(); gContain3.SetValue("+5 낭만의 셔츠 무료 증정!!!"); Debug.Log("게임 소식 메시지 : " + gContain3.GetValue()); GenericContainer<int> gContain4 = new GenericContainer<int>(); gContain4.SetValue(123); Debug.Log("기본 자료형 데이타 : " + gContain4.GetValue()); } }
using UnityEngine; using System.Collections; using System.Collections.Generic; // 스택 클래스(Int형) public class CStackInt { int emptyStack; // 빈스택 인덱스 int[] items; // int 아이템 배열 int top; // 현재 스택 커서(번호) int size; // 스택 크기(아이템 저장 갯수) // 스택 객체 생성자 public CStackInt(int size) { this.size = size; items = new int[size]; emptyStack = -1; top = emptyStack; } public void Push(int item) { items[++top] = item; } public int Pop() { return items[top--]; } public bool IsFull() { return (top + 1) == size; } public bool IsEmpty() { return top == emptyStack; } } // 스택 클래스(Float형) public class CStackFloat { int emptyStack; float[] items; // float 아이템 int top; int size; // 스택 객체 생성자 public CStackFloat(int size) { this.size = size; items = new float[size]; // float 배열 생성 } public void Push(float item) // float 아이템 추가 { items[++top] = item; } public float Pop() // float 아이템 반환 { return items[top--]; } public bool IsFull() { return (top + 1) == size; } public bool IsEmpty() { return top == emptyStack; } } // 스택 클래스(String형) public class CStackString { int emptyStack; string[] items; // float 아이템 int top; int size; // 스택 객체 생성자 public CStackString(int size) { this.size = size; items = new string[size]; // flaot 배열 생성 emptyStack = -1; top = emptyStack; } public void Push(string item) // float 아이템 추가 { items[++top] = item; } public string Pop() // float 아이템 반환 { return items[top--]; } public bool IsFull() { return (top + 1) == size; } public bool IsEmpty() { return top == emptyStack; } } public class CItem { string itemName; public CItem(string itemName) { this.itemName = itemName; } public string GetItemName() { return itemName; } } // 스택 클래스(제네릭) public class CStack<T> { int emptyStack; T[] items; // float 아이템 int top; int size; // 스택 객체 생성자 public CStack(int size) { this.size = size; items = new T[size]; // float 배열 생성 emptyStack = -1; top = emptyStack; } public void Push(T item) // float 아이템 추가 { items[++top] = item; } public T Pop() // flaot 아이템 반환 { return items[top--]; } public bool IsFull() { return (top + 1) == size; } public bool IsEmpty() { return top == emptyStack; } } public class CGenericBase : MonoBehaviour { void Start() { // 제네릭 사용하지 않았을때 스택을 사용하는 코드 /* int size = 5; CStackInt istack = new CStackInt(size); for (int i = 0; i < 7; i++) { if (!istack.IsFull()) { istack.Push(i); } else { Debug.Log("스택이 꽉 찼습니다. [저장 실패 : " + i + "]"); } } while (!istack.IsEmpty()) { Debug.Log(istack.Pop()); } CStackFloat fstack = new CStackFloat(size); for (int i = 0; i < 7; i++) { if (!fstack.IsFull()) { fstack.Push(i + 0.1f); } else { Debug.Log("스택이 꽉 찼습니다. [저장 실패 : " + i + "]"); } } while (!fstack.IsEmpty()) { Debug.Log(fstack.Pop()); } CStackString sstack = new CStackString(size); for (int i = 0; i < 7; i++) { if (!sstack.IsFull()) { sstack.Push(i.ToString()); } else { Debug.Log("스택이 꽉 찼습니다. [저장 실패 : " + i + "]"); } } while (!sstack.IsEmpty()) { Debug.Log(sstack.Pop()); } */ // 제네릭을 사용했을때 스택을 사용하는 코드 /* int size = 5; CStack<int> istack = new CStack<int>(size); for (int i = 0; i < 7; i++) { if (!istack.IsFull()) { istack.Push(i); } else { Debug.Log("스택이 꽉 찼습니다. [저장 실패 : " + i + "]"); } } while (!istack.IsEmpty()) { Debug.Log(istack.Pop()); } CStack<float> fstack = new CStack<float>(size); for (int i = 0; i < 7; i++) { if (!fstack.IsFull()) { fstack.Push(i + 0.1f); } else { Debug.Log("스택이 꽉 찼습니다. [저장 실패 : " + i + "]"); } } while (!fstack.IsEmpty()) { Debug.Log(fstack.Pop()); } CStack<string> sstack = new CStack<string>(size); for (int i = 0; i < 7; i++) { if (!sstack.IsFull()) { sstack.Push(i.ToString()); } else { Debug.Log("스택이 꽉 찼습니다. [저장 실패 : " + i + "]"); } } while (!sstack.IsEmpty()) { Debug.Log(sstack.Pop()); } */ // 제네릭 사용 int size = 3; CStack<CItem> itemStack = new CStack<CItem>(size); itemStack.Push(new CItem("도적의검")); itemStack.Push(new CItem("백색 기사의 방패")); itemStack.Push(new CItem("견습사의 도검")); while (!itemStack.IsEmpty()) { Debug.Log(itemStack.Pop().GetItemName()); } } }
댓글 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] 클래스 | 푸딩뱃살 | 3580 | 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 |
>> | [C#] Generic (제네릭) (with Unity) | 푸딩뱃살 | 4889 | 2015.11.22 12:32 |
172 | [C/C++] Templete (템플릿) | 푸딩뱃살 | 2045 | 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) | 푸딩뱃살 | 5123 | 2015.11.18 22:08 |
168 | [C#] Property (프로퍼티) (with Unity) | 푸딩뱃살 | 2443 | 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 |