polymorphism은
같은 이름의 함수가 다른 기능을 수행할 수 있는 개념이다.
예시 코드:
class Animal:
def __init__(self, name):
self.name = name
def sound(self):
pass # 이 함수는 모든 동물들의 소리를 내기 위해 사용될 것이다.
class Dog(Animal):
def sound(self):
print('Woof!') # Dog 객체에 대한 sound 함수가 정의되었다.
class Cat(Animal):
def sound(self):
print('Meow!') # Cat 객체에 대한 sound 함수가 정의되었다.
dog = Dog('Max')
cat = Cat('Luna')
dog.sound() # Woof!
cat.sound() # Meow!
위의 코드에서 Animal 클래스의 sound 함수는 다른 객체에 따라 다른 기능을 수행하게 하는 역할을 하고 있다. 즉, Animal 클래스의 sound 함수는 polymorphism을 통해 다른 클래스의 객체에 따라 다른 기능을 수행할 수 있게 되었다.
반응형
'기타' 카테고리의 다른 글
[Python] decorate (0) | 2023.01.27 |
---|---|
[Python] Visibility (0) | 2023.01.27 |
[Python] inheritance (0) | 2023.01.27 |
[Python] class (0) | 2023.01.27 |
[Python] lambda & map & reduce (0) | 2023.01.27 |