📍클래스 상속
- 상속받는 클래스를 서브 클래스라고 한다.
- 서브 클래스는 부모 클래스의 프로퍼티와 메서드를 받아오며, 서브 클래스에서 이를 따로 정의할 수 있다
- 클래스 상속으로 코드 중복을 줄일 수 있다
class Animal:
sound = ""
def __init__(self, name):
self.name = name
def speak(self):
print("{sound} I'm {name} {sound}".format(name=self.name, sound=self.sound))
# 서브 클래스 1
class Piglet(Animal):
sound = "Oink!" # 프로퍼티 오버라이딩
# 서브 클래스 2
class Cow(Animal):
sound = "Moooo"
hamlet = Piglet("Hamlet")
hamlet.speak()
milky = Cow("Milky White")
milky.speak()