class

호호빵·2022년 4월 20일
0

class

  • 서로 연관이 있는 변수와 함수의 집합, 동일한 모양을 함수로 찍어냄.

init

  • 생성자(constructor)
  • 사용자가 따로 호출하지 않아도 클래스 객체를 생성할 때 자동으로 호출됨.

method

  • class 내에서 정의되는 함수

멤버변수

  • self.변수명 = 값
  • class 내에서 사용할 수 있는 변수
class Unit:   # class 이름 : Unit
    def __init__(self, name, hp, damage):
        self.name = name
        self.hp = hp
        self.damage = damage
        print("{0} 유닛이 생성되었습니다.".format(self.name))
        print("체력 {0}, 공격력 {1}".format(self.hp, self.damage))

marine1 = Unit("마린", 40, 5)
tank = Unit("탱크", 150, 35)

wraith1 = Unit("레이스", 80, 5)
print("유닛 이름 : {0}, 공격력 : {1}".format(wraith1.name, wraith1.damage))

# 추가 변수 할당
wraith2 = Unit("빼앗은 레이스", 80, 5)
wraith2.clocking = True

if wraith2.clocking == True:
    print("{0}는 현재 클로킹 상태입니다.".format(wraith2.name))

profile
하루에 한 개념씩

0개의 댓글

관련 채용 정보