회원 로그인
|
상속 (with Unity)
상속
- 같은 성질을 가진 여러 클래스들의 공통적인 부분을 재활용하도록 하는 것
[상속의 특징]
1. 공통된 성질을 가진 클래스를 부모(기반) 클래스라고 지칭함
2. 공통된 성질을 물려 받는 클래스를 자식(파생) 클래스라고 지칭함
3. 상속시 기반클래스의 생성자는 파생클래스에서 명시적을 호출해줘야 함
[상속과 접근지정자]
- protected : 상속 상의 클래스들간에만 공유 가능한 접근 지정자
[상속 문법]
public class 기반 클래스 {
protected 멤버들 (변수, 함수)
public 멤버들 (변수, 함수)
}
public class 파생 클래스 : 기반 클래스 {
protected 멤버들 (변수, 함수)
public 멤버들 (변수, 함수)
}
- 같은 성질을 가진 여러 클래스들의 공통적인 부분을 재활용하도록 하는 것
[상속의 특징]
1. 공통된 성질을 가진 클래스를 부모(기반) 클래스라고 지칭함
2. 공통된 성질을 물려 받는 클래스를 자식(파생) 클래스라고 지칭함
3. 상속시 기반클래스의 생성자는 파생클래스에서 명시적을 호출해줘야 함
[상속과 접근지정자]
- protected : 상속 상의 클래스들간에만 공유 가능한 접근 지정자
[상속 문법]
public class 기반 클래스 {
protected 멤버들 (변수, 함수)
public 멤버들 (변수, 함수)
}
public class 파생 클래스 : 기반 클래스 {
protected 멤버들 (변수, 함수)
public 멤버들 (변수, 함수)
}
using UnityEngine; using System.Collections; public class CommonUnit { protected string _name; protected int _hp; protected int _damage; public CommonUnit(string name, int damage) { _name = name; _damage = damage; } public void Move() { Debug.Log("[" + _name + "] 님이 이동을 합니다. "); } public void Attack() { Debug.Log("[" + _name + "] 님이 " + _damage + "만큼의 공격을 합니다. "); } public void Hit(int damage) { Debug.Log("[" + _name + "] 님이 " + damage + " 데미지를 입었습니다. "); } public void Die() { Debug.Log("[" + _name + "] 님이 사망했습니다. "); } } public class Player : CommonUnit { private int _level; private int _gravity; private string _talkMessage; public Player(string name, int damage, string talkMessage) : base(name, damage) { _talkMessage = talkMessage; Debug.Log("[" + _name + "] 님이 접속하였습니다."); Talk(); } public void Talk() { Debug.Log("[" + _name + "] 메시지 : " + _talkMessage); } void Jump() { Debug.Log("[" + _name + "] 님이 점프를했습니다. "); } } public class Monster : CommonUnit { private int _skillType; public Monster(string name, int damage, int skillType) : base(name, damage) { _skillType = skillType; Debug.Log("[" + _name + "] 님이 나타났습니다."); } public void Skill() { Debug.Log("[" + _name + "] 님이 " + _skillType + "타입의 스킬을 사용합니다. "); } } public class Creature : CommonUnit { private int _time; public Creature(string name, int damage, int time) : base(name, damage) { _time = time; Debug.Log("[" + _name + "] 님이 소환 됐습니다."); Timer(); } void Timer() { Debug.Log("[" + _name + "] 님이 " + _time + " 시간뒤에 소멸됩니다."); } }; public class ExtendsStudy : MonoBehaviour { // Use this for initialization void Start() { Player player = new Player("티거", 30, "저는 푸우의 친구 티거입니다!!"); player.Move(); player.Attack(); player.Hit(10); player.Die(); Monster monster = new Monster("오우거", 20, 2); monster.Move(); monster.Attack(); monster.Skill(); monster.Hit(30); monster.Die(); Creature creature = new Creature("멀록", 5, 10); creature.Move(); creature.Attack(); creature.Hit(5); creature.Die(); } // Update is called once per frame void Update() { } }
댓글 0개
| 엮인글 0개
285개(1/15페이지)
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
285 | [Python] 동적 import - 모듈을 변수로 받아오기 | 푸딩뱃살 | 412 | 2022.10.27 10:45 |
284 | [Python] 파이썬 3.7.7과 3.9.7의 os.path.expanduser() 차이 | 푸딩뱃살 | 455 | 2022.08.18 12:22 |
283 | [Python] error: Microsoft Visual C++ 9.0 is required. | 푸딩뱃살 | 684 | 2022.08.03 13:35 |
282 | [Python] pyscript | 푸딩뱃살 | 452 | 2022.06.09 11:21 |
281 | [Python] float is / float not is | 푸딩뱃살 | 593 | 2022.03.02 15:03 |
280 | [Python] 이터널 문자열 f | 푸딩뱃살 | 833 | 2022.01.27 16:35 |
279 | [Python] is와 ==의 차이 | 푸딩뱃살 | 485 | 2021.11.25 15:54 |
278 |
[Python] Error: ImportError: file |
푸딩뱃살 | 916 | 2021.11.16 11:24 |
277 | [Python] 파이썬 디컴파일 - uncompyle6 | 푸딩뱃살 | 774 | 2021.11.10 14:46 |
276 | [Python] 파이썬 확장자 설명 | 푸딩뱃살 | 550 | 2021.11.03 14:38 |
275 | [참고] 웹 fbx 뷰어 | 푸딩뱃살 | 470 | 2021.10.19 15:46 |
274 | [Python] enumerate() | 푸딩뱃살 | 500 | 2021.10.13 14:44 |
273 | [Python] 아나콘다에서 가상 환경 | 푸딩뱃살 | 719 | 2020.11.21 00:26 |
272 | [Python] pip로 설치 때 퍼미션 에러 | 푸딩뱃살 | 1264 | 2020.06.06 17:13 |
271 | [Python] OpenCV 10-3. 이미지 Thresholding - Otsu's Binarizatio | 푸딩뱃살 | 673 | 2020.06.05 14:01 |
270 | [Python] OpenCV 10-2. 이미지 Thresholding - Adaptive Threshold | 푸딩뱃살 | 694 | 2020.06.05 13:58 |
269 | [Python] OpenCV 10-1. 이미지 Thresholding | 푸딩뱃살 | 578 | 2020.06.05 13:56 |
268 | [Python] OpenCV 9-2. 색 추적 | 푸딩뱃살 | 772 | 2020.06.02 23:29 |
267 | [Python] OpenCV 9-1. 색공간 바꾸기 | 푸딩뱃살 | 639 | 2020.06.02 23:27 |
266 | [Python] OpenCV 8-3. 이미지 비트 연산 | 푸딩뱃살 | 528 | 2020.06.02 23:21 |