20210111 TIL
'''
** Scenario **
Unit (Marine, Medic, DropShip)
Marine - 4명
Medic - 2명
DropShip - 6명을 태워서 특정 지점을 공격
예제를 통한 상속 [실습]
'''
# parent class
class Unit(object) :
def __init__(self, damage, life):
# 생성된 인스턴스의 타입을 확인하는 코드
self.utype = self.__class__.__name__
# 생성자 선언
self.damage = damage
self.life = life
def unitInfo(self):
print('타입 {}'.format(self.utype))
print('공격력 {}'.format(self.damage))
print('생명력 {}'.format(self.life))
def attack(self):
pass
# child class
class Marine(Unit) :
def __init__(self, damage, life, offenseUpgrade, defenseUpgrade):
super().__init__(damage, life) # 부모의 구성요소를 참조하고
self.offenseUpgrade = offenseUpgrade # 자녀 클래스만에 해당하는 것들 생성
self.defenseUpgrade = defenseUpgrade # 자녀 클래스만에 해당하는 것들 생성
def UnitInfo(self): # 오버라이딩
super().unitInfo() # 부모가 가진 unitInfo 함수를 호출 (오버라이딩 메서드)
print('공격력 업그레이드 {}'.format(self.offenseUpgrade))
print('방어력 업그레이드 {}'.format(self.defenseUpgrade))
def attack(self): # 오버라이딩
print('마린 공격 개시...빵야빵야빵야')
def steampack(self):
if self.life > 50 :
self.damage *= 1.5
self.life -= 10
print('마린 steampack 사용')
else :
print('체력이 낮아 스팀팩을 사용할 수 없습니다.')
class Medic(Unit) :
def __init__(self, damage, life, defenseUpgrade):
super().__init__(damage, life)
self.defenseUpgrade = defenseUpgrade
def unitInfo(self): # 오버라이딩
super().unitInfo()
print('방어력 업그레이드 {}'.format(self.defenseUpgrade))
def attack(self): # 오버라이딩
print('메딕이 마린을 치료합니다.')
class DropShip(Unit) :
def __init__(self, damage, life, defenseUpgrade):
super().__init__(damage, life)
self.defenseUpgrade = defenseUpgrade
def unitInfo(self):
super().unitInfo()
print('방어력 업그레이드 {}'.format(self.defenseUpgrade))
def attack(self):
print('목표지점으로 이동합니다.')
def board(self, unitType):
self.unitType = unitType
print('부대원을 태웠습니다.')
def drop(self):
print('모든 부대원(유닛)을 DropShip에서 내립니다.')
return self.unitType
Caller
from ai.oop.oopExec import *
# unit = Unit(10, 100)
# unit.unitInfo()
# 필요에 의해서 타입을 체크하고 활용이 가능하다.
# if unit.utype == 'Unit' :
# print(True)
# else :
# print(False)
# Marine 생성
marine01 = Marine(10, 100, 0, 0)
marine02 = Marine(10, 100, 0, 0)
marine03 = Marine(10, 100, 0, 0)
marine04 = Marine(10, 100, 0, 0)
# Medic 생성
medic01 = Medic(0, 100, 0)
medic02 = Medic(0, 100, 0)
# 병력을 list에 담기
# 리스트 만드는 방법
# troopList = list()
# troopList = []
# troopList.append()
troopList = []
troopList.append(marine01)
troopList.append(marine02)
troopList.append(marine03)
troopList.append(marine04)
troopList.append(medic01)
troopList.append(medic02)
# 기본정보 출력
# return 타입이 없는 걸 출력하면 None이 출력됨.
for obj in troopList :
#print(obj) # 주소번지가 찍힘
obj.unitInfo()
print("*" * 50)
# DropShip 생성
ship = DropShip(0, 50, 0)
# 부대원 탑승
ship.board(troopList) # board에 unitType이라는 변수에 list 형식의 데이터가 assign 됨.
# 공격지점 설정
ship.attack() # return 이 print로 되어있어서 print로 출력된다.
# 부대원을 내린다면?
troopAttackList = ship.drop() # return 타입이 self.unitType으로 list 형식의 데이터가 할당됨
# 공격
for unit in troopAttackList : # list 형식의 데이터에서
if isinstance(unit, Marine) : # unit(troppAttackList 에서 가져온 값)이 Marine이라면
unit.steampack() # Marine의 함수인 steampack 사용이 가능해진다.
unit.attack()
'AI' 카테고리의 다른 글
[K-digital] 프로젝트형 AI 서비스 개발 교육 Day 8 (0) | 2021.01.13 |
---|---|
[K-digital] 프로젝트형 AI 서비스 개발 교육 Day 7 (0) | 2021.01.13 |
[K-digital] 프로젝트형 AI 서비스 개발 교육 Day 5 (0) | 2021.01.11 |
[K-digital] 프로젝트형 AI 서비스 개발 교육 Day 4-3 (0) | 2021.01.07 |
[K-digital] 프로젝트형 AI 서비스 개발 교육 Day 4-2 (0) | 2021.01.07 |