출처: https://astrocosmos.tistory.com/202 [ASTROCOSMOS:티스토리] 'Python - 기초 데이터 타입, 문법' 카테고리의 글 목록 :: 하나둘셋넷
728x90

클래스 개념

class, self, __init__() 클래스는 사용자 정의 데이터 타입
   
클래스선언(코드작성) class Marine:
    health, ap = 40, 5
    def attack(self, target) :
        target.health -= self.ap
   
객체 생성 m1, m2 = Marine(), Marine()
m1.health, m1.ap, m2.health, m2.ap
   
생성자 메서드 스페셜 메서드 중 하나(특별한 기능을 하는 앞뒤로 __가 있는 메서드)

__init__() : 객체를 생성할 때 실행되는 메서드
   
클래스 선언(코드 작성) class Marine:
    def __init__(self, health, ap =5):
        self.health, self.ap = health, ap

    def attack(self, target):
        target, health -= self.ap
   
728x90

+ Recent posts