2.2다형성
다형성에 관해 좀 더 자세히 알아보겠습니다. 우선 간단한 예제부터 시작합시다.
코드 7-10 oop2/oop2_2/polymorphism_1.py
class Animal: def eat(self): #1 print('eat something') class Lion(Animal): def eat(self): #2 print('eat meat') class Deer(Animal): def eat(self): #3 print('eat grass') class Human(Animal): def eat(self): #4 print('eat meat and grass') if __name__ = = "__main__": animals = [ ] #5 animals.append(Lion()) animals.append(Deer()) animals.append(Human()) for animal in animals: animal.eat() #6
실행결과 eat meat
eat grass
eat meat and grass