[python] super()

덴장·2024년 7월 19일

python

목록 보기
24/30

##오버라이딩
class Unit:

def __init__(self, name, hp ,speed): #python의 생성자 __init__
    self.name = name     # 멤버변수
    self.hp = hp         # 멤버변수
    self.speed = speed   # 멤버변수

def move(self , location):
    print("[지상유닛 이동]")
    print("{0} : {1} 방향으로 이동합니다.[속도 {2}]"\
    .format(self.name, location , self.speed))

class AttackUnit(Unit):

def __init__(self, name, hp , speed, damage): #python의 생성자 __init__
    Unit.__init__(self , name, hp , speed) # 클래스 상속
    self.damage = damage # 멤버변수 
    ## 멤버변수 - 클래스 내 생성된 변수
    print("{0} 유닛이 생성되었습니다.".format(self.name))
    print("체력 {0}, 공격력 {1}".format(self.hp, self.damage))


def attack(self , location):
    print("{0} : {1} 방향으로 적군을 공격합니다. [공격력 {2}]" \
    .format(self.name , location , self.damage))
        
def damaged(self , damage):
    print("{0} : {1} 데미지를 받음." \
              .format(self.name  , self.damage))
    self.hp -= damage
    print("{0} : 현재체력은 {1} 입니다.".format(self.name  , self.hp))
    if self.hp <=0:
         print("{0} : 파괴.".format(self.name))
         

#################### 다중상속 ################################
#비행유닛
class Flyable:

def __init__(self , flying_speed):
    self.flying_speed = flying_speed

def fly(self , name , location):
    print("{0} : {1} 방향으로 날아갑니다. [속도 {2}]"\
          .format(name , location , self.flying_speed))

#공격가능 비행유닛
class FlyableAttackUnit(AttackUnit , Flyable):

def __init__(self , name, hp , damage , flying_speed):
    AttackUnit.__init__(self , name , hp , "0", damage) # tmvlemsms 0으로
    Flyable.__init__(self , flying_speed )
#Unit 함수 오버라이딩
def move(self , location):
    print("[공중 유닛 이동]")
    self.fly(self.name , location)

############################################################################
#건물
class BulidingUnit(Unit):

def __init__(self , name , hp , location):
    # 클래스 상속 super().__init__(name, hp , 0) 같음.
    #Unit.__init__(self , name, hp , 0) # 아래와 같은 기능이므로 주석처리
    
    # Unit.__init__(self , name, hp , 0) 가 아래와 같은 역할
    super().__init__(name, hp , 0) 
    self.location = location # 멤버변수
    
    pass # pass 가 존재하는경우 미완성 상태라도 일단 넘어감.
    

supply_depot = BulidingUnit( "건물", 500 , "7시")

profile
개발자

0개의 댓글