python 类函数多态 示例
时间: 2023-10-13 10:20:22 浏览: 54
好的,关于 Python 中的多态,我们可以举一个简单的例子。假设我们有一个 Animal 类,里面有一个名为 sound 的方法,它用来输出动物的叫声。现在我们派生出一个 Dog 类和一个 Cat 类,它们都继承自 Animal,并且都覆盖了 sound 方法。这里就体现出了多态的概念,因为无论我们调用 sound 方法时是用 Dog 类对象还是 Cat 类对象,都会触发相应的方法,输出对应动物的叫声。例如,我们可以这样写代码:
```
class Animal:
def sound(self):
pass
class Dog(Animal):
def sound(self):
print("汪汪汪")
class Cat(Animal):
def sound(self):
print("喵喵喵")
def main():
animals = [Dog(), Cat()]
for animal in animals:
animal.sound()
if __name__ == "__main__":
main()
```
输出:
```
汪汪汪
喵喵喵
```
这就是一个简单的多态示例,即在多个不同的类中有着相同的方法名,但是实现方式不同。
相关问题
类的多态的示例
好的,这是一个类的多态示例:
```python
class Animal:
def __init__(self, name):
self.name = name
def make_sound(self):
pass
class Dog(Animal):
def make_sound(self):
return "Woof!"
class Cat(Animal):
def make_sound(self):
return "Meow!"
class Bird(Animal):
def make_sound(self):
return "Tweet!"
def animal_sound(animal):
print(animal.make_sound())
dog = Dog("Rex")
cat = Cat("Whiskers")
bird = Bird("Tweety")
animal_sound(dog) # Output: "Woof!"
animal_sound(cat) # Output: "Meow!"
animal_sound(bird) # Output: "Tweet!"
```
在这个示例中,`Animal` 是一个基类,包含一个 `make_sound` 方法,但是这个方法没有实现。`Dog`、`Cat` 和 `Bird` 都是继承自 `Animal` 的子类,并且都实现了自己的 `make_sound` 方法。`animal_sound` 函数接受一个 `Animal` 对象作为参数,并调用传入对象的 `make_sound` 方法。由于每个子类都实现了自己的 `make_sound` 方法,所以 `animal_sound` 函数可以在运行时根据传入的对象的实际类型来确定调用哪个方法。这就是多态的体现。
python多态的实现
Python中的多态是通过继承和方法重写来实现的。在Python中,可以定义一个基类,然后派生出多个子类,每个子类都可以重写基类的方法,从而实现多态性。
多态的概念是应用于Java和C#这一类强类型语言中,而Python则采用了"鸭子类型"的方式,即只要一个对象具有特定的方法或属性,就可以当作相应类型的对象使用。这使得Python的多态实现更加灵活。
举个例子来说,假设有一个动物类Animal,它有一个方法func。然后派生出猫类Cat和狗类Dog,它们分别重写了func方法。此时,我们可以通过调用func方法来实现多态。无论传入的是一个Animal对象、Cat对象还是Dog对象,都可以调用相应的func方法。
在Python中,多态的实现并没有严格的类型限制,因为Python中函数的参数并没有类型限制。但是通过继承和方法重写,我们仍然可以实现类似于多态的效果。
请参考下面的示例代码来理解Python中多态的实现:
```python
class Animal:
def func(self):
print('动物发出了声音')
class Cat(Animal):
def func(self):
print('喵 喵 喵')
class Dog(Animal):
def func(self):
print('汪 汪 汪')
def work(animal):
animal.func()
work(Animal()) # 输出:"动物发出了声音"
work(Cat()) # 输出:"喵 喵 喵"
work(Dog()) # 输出:"汪 汪 汪"
```
在上面的示例中,我们定义了一个work函数,它接受一个参数animal。无论传入的是Animal对象、Cat对象还是Dog对象,都可以调用相应的func方法,实现了多态的效果。
需要注意的是,Python中的多态并没有严格的类型检查,所以在编写代码时需要注意传入的参数是否具有相应的方法或属性。
希望这个例子可以帮助你理解Python中多态的实现原理。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)