专业编程基础技术教程

网站首页 > 基础教程 正文

python 继承super() 子类中调用父类的6种方法

ccvgpt 2024-07-26 00:41:00 基础教程 11 ℃

总结不易,欢迎收藏转发,欢迎留言讨论和补充。可以的话,帮忙点个关注不迷路。

super() 是 Python 内置函数,用于在子类中调用父类的方法。

python 继承super() 子类中调用父类的6种方法

1、调用父类的构造方法

子类在实例化时需要调用父类的构造方法,以初始化从父类继承的属性和方法。可以使用 super() 来调用父类的构造方法:

class Parent:
    def __init__(self, arg1):
        self.arg1 = arg1

class Child(Parent):
    def __init__(self, arg1, arg2):
        super().__init__(arg1)
        self.arg2 = arg2

child = Child("parent", "child")
print(child.arg1)  # arg1
print(child.arg2)  # arg2
  1. 调用父类的方法

子类可以通过 super() 调用父类的方法,以获得从父类继承的功能。在调用时需要指定调用的方法名和参数:

class Parent:
    def my_method(self, arg1, arg2):
        return arg1 + arg2
        
class Child(Parent):
    def my_method(self, arg1, arg2):
        result = super().my_method(arg1, arg2)
        return result * 2
        
child = Child()
print(child.my_method(1, 2))  # 
  1. 多重继承中的使用

在多重继承的情况下,子类可以使用 super() 调用所有父类的方法,以避免重复调用相同的方法。在调用时需要指定调用的父类和方法名:

class Animal:
    def __init__(self, name):
        self.name = name

    def eat(self):
        print(f"{self.name} is eating.")


class Flyable:
    def fly(self):
        print(f"{self.name} is flying.")


class Bird(Animal, Flyable):
    def __init__(self, name):
        super().__init__(name)


bird = Bird("Sparrow")
bird.eat()
bird.fly()
#Sparrow is eating.
#Sparrow is flying.
  1. 动态绑定

Super() 还可以动态绑定,在运行时根据当前实例和方法动态决定调用哪个父类的方法:

class Parent:
    def my_method(self, arg1):
        return arg1 + 1
        
class Child(Parent):
    def my_method(self, arg1):
        result = super().my_method(arg1)
        return result * 2
        
class Grandchild(Child):
    def my_method(self, arg1):
        result = super().my_method(arg1)
        return result * 3
        
grandchild = Grandchild()
print(grandchild.my_method(1))  # 12
  1. 用 super() 调用类方法

在 Python 中,可以使用 super() 来调用父类的静态方法和类方法。这些方法不需要实例化类,因此无需使用 self 参数。调用方式与调用实例方法相同,只需将方法名传递给 super() 即可:

class Parent:
     @classmethod
    def class_method(cls, arg1):
        return arg1 + 1

class Child(Parent):
     @classmethod
    def class_method(cls, arg1):
        result = super().class_method(arg1)
        return result * 2

print(Child.class_method(1))  # 4
  1. 用 super() 调用特殊方法

Python 中有许多特殊方法(也称为魔术方法),例如 __str__、__repr__、__len__ 等等。这些方法是类的基本功能之一,也是 Python 对象的特殊行为和语义的重要来源。可以使用 super() 来调用父类的特殊方法,以获得从父类继承的特殊行为和语义

class Parent:
    def __str__(self):
        return "Parent"

class Child(Parent):
    def __str__(self):
        return super().__str__() + " -> Child"

child = Child()
print(str(child))  # Parent -> Child

需要注意的是,super() 只能用于新式类,即继承自 object 的类。如果子类没有显式地继承自 object,则 super() 将无法调用父类的方法。同时,如果父类的方法是私有方法,即以双下划线 __ 开头的方法,那么 super() 也无法调用该方法。

??问题:静态方法怎么继承。

??这个程序报错实例化打印的时候 ;AttributeError: 'super' object has no attribute 'my_method ?

class Parent1:
    def my_method(self, arg1):
        return arg1 + 1
        
class Parent2:
    def my_method(self, arg2):
        return arg2 + 2
        
class Child(Parent1, Parent2):
    def my_method(self, arg1, arg2):
        result1 = super(Parent1, self).my_method(arg1)
        result2 = super(Parent2, self).my_method(arg2)
        return result1 + result2
        
child = Child()
print(child.my_method(1, 2))  # 6 ,

Tags:

最近发表
标签列表