객체마다 다르게 가지는 속성
def __init__(self, name,hp, shield, demage):
self.name = name
self.hp = hp
self.shield = shield
self.demage = demage
# self.name, self.hp, self.shield, self.demage 가 인스턴스 속성이다.
클래스 안에서 인스턴스 속성을 사용할 때는 "self.속성명" 으로 사용
클래스 밖에서 인스턴스 속성을 사용할 때는 "객체명.속성명"
모든 객체가 공유하는 속성
class Unit:
"""
속성 : 이름, 체력, 방어막, 공격력
"""
# 생성자 (constructor)
# 객체를 생성할 때 호출되는 메서드
count = 0
def __init__(self, name,hp, shield, demage):
self.name = name
self.hp = hp
self.shield = shield
self.demage = demage
Unit.count += 1
count가 클래스 속성에 해당한다.
클래스 안에서만 접근 가능한 속성
self.__hp = hp #hp 값이 고정된 값으로 출력
#네임 맹글링 (name magling)
probe._Unit__hp = 9999
print(probe)
네임 맹글링? - 파이썬이 변수/함수의 이름을 짓이겨서 다른 이름으로 바꿔버리는 것으로 클래스의 속성값을 외부에서 접근하기 힘들게 할 때, 하위 클래스가 상위클래스의 속성을 오버라이딩 하는 것을 막을 때 사용한다.