本文目录导读:

在Python中调用父类方法主要有两种方式,我来详细介绍:
使用 super() 函数(推荐方式)
这是最常用和推荐的方式,特别是多重继承时:
class Animal:
def __init__(self, name):
self.name = name
print(f"Animal __init__: {name}")
def speak(self):
return f"{self.name} makes a sound"
class Dog(Animal):
def __init__(self, name, breed):
# 使用 super() 调用父类 __init__
super().__init__(name)
self.breed = breed
print(f"Dog __init__: {breed}")
def speak(self):
# 调用父类方法并扩展
parent_sound = super().speak()
return f"{parent_sound}, but specifically woof!"
# 使用示例
dog = Dog("Buddy", "Golden Retriever")
print(dog.speak())
# 输出:
# Animal __init__: Buddy
# Dog __init__: Golden Retriever
# Buddy makes a sound, but specifically woof!
直接使用父类名称调用
class Animal:
def eat(self):
return "Eating food"
class Cat(Animal):
def eat(self):
# 直接通过父类名调用
result = Animal.eat(self) # 注意要传递 self
return f"{result} like a cat"
cat = Cat()
print(cat.eat()) # 输出: Eating food like a cat
调用父类的类方法
class Parent:
@classmethod
def class_method(cls):
return f"Parent class method from {cls.__name__}"
class Child(Parent):
@classmethod
def class_method(cls):
# 调用父类类方法
parent_result = super().class_method()
return f"Child modified: {parent_result}"
print(Child.class_method())
# 输出: Child modified: Parent class method from Child
调用父类的静态方法
class Parent:
@staticmethod
def static_method():
return "Parent static method"
class Child(Parent):
@staticmethod
def static_method():
parent_result = super(Child, Child).static_method() # 方式1
# 或者
# parent_result = Parent.static_method() # 方式2
return f"Child: {parent_result}"
print(Child.static_method())
# 输出: Child: Parent static method
多重继承中的使用
class A:
def method(self):
return "A"
class B:
def method(self):
return "B"
class C(A, B):
def method(self):
# 按 MRO 顺序调用第一个父类
result = super().method()
return f"C + {result}"
def call_b_method(self):
# 调用特定父类方法
return B.method(self)
c = C()
print(c.method()) # 输出: C + A (因为 MRO 是 C -> A -> B)
print(c.call_b_method()) # 输出: B
重要注意事项
MRO(方法解析顺序)
class D(C, A): # 注意顺序
pass
print(D.mro()) # 查看 MRO
# 输出: [D, C, A, B, object]
常见错误示例
# 错误:忘记传递 self 参数
class Child(Parent):
def method(self):
# return Parent.method() # 错误!
return Parent.method(self) # 正确
实际应用示例
class Vehicle:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def info(self):
return f"{self.brand} {self.model}"
class Car(Vehicle):
def __init__(self, brand, model, doors):
# 先调用父类 __init__
super().__init__(brand, model)
self.doors = doors
def info(self):
# 扩展父类方法
base_info = super().info()
return f"{base_info} with {self.doors} doors"
# 使用
car = Car("Toyota", "Camry", 4)
print(car.info()) # 输出: Toyota Camry with 4 doors
推荐使用 super() 函数,它:
- 自动处理 MRO
- 支持多重继承
- 不需要硬编码父类名称
- 代码更简洁、易维护
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播