개발 => 복습 후 재정리 대기/Python

[Python][문법] class, instance

장 상 현 2021. 5. 6.

객 체 지 향 ?

class Monster():
    hp = 100
    alive = True

    def damage(self, attack):
        self.hp = self.hp - attack
        if self.hp < 0:
            self.alive = False

    def status_check(self):
        if self.alive == True: # True 생략가능
            print('살았다!')
        else:
            print('죽었다!')

m1 = Monster() # m1? 인스턴스? 몬스터 복제!?
m1.damage(150)
m1.status_check() # 죽었다!

m2 = Monster()
m2.damage(90)
m2.status_check() # 살았다!

class Monster():
    hp = 100
    alive = True

    def damage(self, attack):
        self.hp = self.hp - attack
        if self.hp < 0:
            self.alive = False

    def status_check(self):
        if self.alive == True: # True 생략가능
            print('살았다!')
        else:
            print('죽었다!')

m1 = Monster() # m1? 인스턴스? 몬스터 복제!?
m1.damage(150)
m1.status_check() # 죽었다!

m2 = Monster()
m2.damage(90)
m2.status_check() # 살았다!

댓글