多态
探索多态如何实现灵活且动态的代码
多态 是 CoddyKit 上的免费 Learn AI with Python 课时。 这是第 4 节课,共 6 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Learn AI with Python 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Learn AI with Python 课程共包含 6 节课。
多态简介
多态简介
多态是面向对象编程(OOP)中的核心概念,它允许将不同类的对象视为共同父类的对象。它为代码提供了灵活性和可复用性。
在本课中,您将学习如何在 Python 中使用方法和类实现多态。

什么是多态
什么是多态
多态允许不同类中的方法使用相同的名称,同时表现出不同的行为。这样可以让代码更加灵活,也更易于扩展。
# Defining a superclass
class Animal:
# Method in the superclass
def speak(self):
print("Animal speaks") # Generic message for all animals
# Defining a subclass that overrides the speak method
class Dog(Animal):
def speak(self):
print("Dog barks") # Specific behavior for Dog使用方法实现多态
使用方法实现多态
不同类中名称相同的方法可以提供不同的实现。
# Another subclass with its own implementation of the speak method
class Cat(Animal):
def speak(self):
print("Cat meows") # Specific behavior for Cat
# Function demonstrating polymorphism
# It can accept objects of any subclass of Animal
def animal_sound(animal):
animal.speak() # Calls the appropriate speak method based on the object type
# Creating objects of Dog and Cat
dog = Dog()
cat = Cat()
# Calling the function with different objects
animal_sound(dog) # Outputs: Dog barks
animal_sound(cat) # Outputs: Cat meows方法多态示例
使用方法实现多态
不同类中名称相同的方法可以提供不同的实现。
# Another subclass with its own implementation of the speak method
class Cat(Animal):
def speak(self):
print("Cat meows") # Specific behavior for Cat
# Function demonstrating polymorphism
# It can accept objects of any subclass of Animal
def animal_sound(animal):
animal.speak() # Calls the appropriate speak method based on the object type
# Creating objects of Dog and Cat
dog = Dog()
cat = Cat()
# Calling the function with different objects
animal_sound(dog) # Outputs: Dog barks
animal_sound(cat) # Outputs: Cat meows使用函数实现多态
使用函数实现多态
多态允许同一个函数对不同类型的对象执行操作。
# Defining two unrelated classes
class Bird:
def fly(self):
print("Bird flies") # Behavior specific to birds
class Plane:
def fly(self):
print("Plane flies") # Behavior specific to planes
# Function demonstrating polymorphism
# It can accept objects of both Bird and Plane
def flying_object(obj):
obj.fly() # Calls the appropriate fly method based on the object type
# Creating objects of Bird and Plane
bird = Bird()
plane = Plane()
# Calling the function with different objects
flying_object(bird) # Outputs: Bird flies
flying_object(plane) # Outputs: Plane flies继承中的多态
继承中的多态
多态通常通过继承实现,在继承关系中,子类会重写父类的方法。
# Superclass representing a generic vehicle
class Vehicle:
def start(self):
print("Vehicle starts") # Generic behavior for all vehicles
# Subclass representing a specific type of vehicle
class Car(Vehicle):
def start(self):
print("Car starts") # Specific behavior for cars
# Subclass representing another specific type of vehicle
class Bike(Vehicle):
def start(self):
print("Bike starts") # Specific behavior for bikes
# Function demonstrating polymorphism
# It can accept objects of any subclass of Vehicle
def start_vehicle(vehicle):
vehicle.start() # Calls the appropriate start method based on the object type
# Creating objects of Car and Bike
car = Car()
bike = Bike()
# Calling the function with different objects
start_vehicle(car) # Outputs: Car starts
start_vehicle(bike) # Outputs: Bike starts多态的优势
多态的优势
多态具有以下几项优势:
- 提高代码的可复用性和灵活性。
- 改善代码的可读性和组织性。
- 鼓励使用通用接口。
现实示例
现实示例
下面通过一个现实示例了解多态的应用。
# Real-world example of polymorphism
class Developer:
def work(self):
print("Writing code") # Specific task for developers
class Designer:
def work(self):
print("Designing user interfaces") # Specific task for designers
# Function demonstrating polymorphism
# It can accept objects of both Developer and Designer
def workday(employee):
employee.work() # Calls the appropriate work method based on the object type
# Creating objects of Developer and Designer
developer = Developer()
designer = Designer()
# Calling the function with different objects
workday(developer) # Outputs: Writing code
workday(designer) # Outputs: Designing user interfaces多态中的常见错误
多态中的常见错误
以下是需要避免的一些错误:
- 需要时忘记在子类中重写方法。
- 在多重继承中混淆方法解析顺序(MRO)。
- 对简单任务不必要地使用多态。
我们学到了什么?
我们学到了什么?
在本课中,您学习了:
- 多态的含义,以及它在 Python 中的工作方式。
- 如何通过方法和继承实现多态。
- 在面向对象编程中使用多态的优势。
做得很好!让我们进入下一个主题。

常见问题解答
「多态」课时是免费的吗?
是的 — 「多态」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Learn AI with Python 课程的其余内容,请升级到 CoddyKit PRO。 Learn AI with Python 课程共包含 6 节课。
「多态」这节课中我会学到什么?
探索多态如何实现灵活且动态的代码 你通过在浏览器中直接运行的动手代码来练习 Learn AI with Python,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Learn AI with Python 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Learn AI with Python 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 6 节。
「多态」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Learn AI with Python 课中编写并运行代码吗?
能。每节 Learn AI with Python 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。