SOLID 원칙의 open closed 원칙과 일치하는 내용이다
class Animal:
def speak(self):
pass
class Cat(Animal):
def speak(self):
print("meow")
class Lion(Animal):
def speak(self):
print("roar")
def makeSpeak(animal:Animal):
animal.speak()
def createAnimal(input_str:str)->Animal:
if input_str == "cat":
return Cat()
elif input_str == "lion":
return Lion()
-------------console-------------------
input_str = input('choose animal: ')
animal = createAnimal(input_str)
makeSpeak(animal)