# 클래스: 객체를 만들기 위한 설계도
# 객체: 설계도로부터 만들어낸 제품
class Monster:
total = 0
# 생성자: 인스턴스 생성시 가장 먼저 호출되는 메서드
def __init__(self, num):
self.age = num
# 클래스 변수: 생성된 인스턴스가 모두 공유할 수 있는 데이터
# (예시 변수 total 은 인스턴스가 생성될 때마다 데이터가 1씩 증가하게끔 설정)
Monster.total += 1
# 메서드 1
def say(self):
print("i'm monster")
# 메서드 2
def clac(self, arg):
self.age -= arg
return self.age
ghost = Monster(500)
ghost.say()
# __dir__: 해당 객체에서 사용할 수 있는 메서드를 모두 출력
print(ghost.__dir__())
print(ghost.clac(100))
# 상속에 대히여
class Robot(Monster):
# 그냥 넘길때
pass
class Dragon(Monster):
# 메서드 오버라이딩: 기존 메서드를 새로 정의
def clac(self, arg):
self.age += arg
return self.age
robot = Robot(200)
print(robot.clac(100))
dragon = Dragon(200)
print(dragon.clac(100))
class Human(Monster):
# 생성자 오버라이딩
def __init__(self, arg, word):
# 기존 생성자를 호출할 수 있다
super().__init__(arg)
self.name = word
castie = Human(50, 'castie')
print(castie.name)
print(castie.total)
해당 내용은 패스트 캠퍼스 권기준 강사님의 강의 내용을 바탕으로 작성되었으며 좋은 강의 감사합니다