简单的说super().init(),就是继承父类的init方法,同样可以使用super()去继承其他方法。 super不是关键字,而是一个类, 调用super()会创建一个super对象: 实例 单继承 class Father: def __init__(self,name='Tom'): self.name=name class Son1(Father): pass class Son2(Father): def __init__(self...
__init__(name, mode, number) # 继承super class __init__属性 def desc(self): print( f'This is {self.name}_{self.mode}_{self.number} switch. made in {self.zone}') print(f'New feature: {self.l3protocol}') if __name__ == '__main__': huawei = Switch('HUAWEI',...
class Animal:(tab)def __init__(self, name):(tab)(tab)self.name = name(tab)def make_sound(self):(tab)(tab)passclass Dog(Animal):(tab)def __init__(self, name):(tab)(tab)super().__init__(name)(tab)def make_sound(self):(tab)(tab)return "Woof!"class Cat(Animal):(tab)def...
class Manager(Employee, Customer): def __init__(self, salary, favorite, address): super().__init__(salary) # super(Manager, self).__init__(salary) 作用同上, # super调用父类的方法,由于继承了两个类,super根据mro顺序来确定调用哪个父类,子类继承写在前面的类 # 使用未绑定的方式调用Customer...
def __init__(self, name): self.name = name print("create an instance of:", self.__class__.__name__) print("name attribute is:", self.name) class Child(Parent): def __init__(self): print("call __init__ from Child class") super(Child,self).__init__("data from Child")...
super()函数的用法如下: classSubClass(ParentClass):def__init__(self,*args,**kwargs):super().__init__(*args,**kwargs) 1. 2. 3. 在上面的代码中,SubClass是子类,ParentClass是父类。在子类的构造函数中,通过调用super().__init__(*args, **kwargs)来调用父类的构造函数。
先分别看看python里的__init__()和super()的意思 先介绍__init__() 在学习 Python 类的时候,总会碰见书上的类中有 __init__() 这样一个方法【注:特殊方法(魔法函数)之一:初始化方法,相当于其它语言的构造函数,故也称为构造方法】,__init__() 方法可以包含多个参数,但必须包含一个名为 self 的参数,且...
(A):def__init__(self):self.n=4defadd(self,m):# 第三步# 来自 B.add 中的 super# self == d, self.n == d.n == 5print('self is {0} @C.add'.format(self))# 等价于 suepr(C, self).add(m)# self 的 MRO 是 [D, B, C, A, object]# 从 C 之后的 [A, object] 中...
deffunc(self):return1# 定义子类classnew(initial):def__init__(self):print('This print is from new object')# 打印子类函数值print(self.func())# 执行父类初始化函数super(new,self).__init__()# 打印父类参数值print(self.param)self.param=4# 定义子类函数 ...
postion[1] + y) class Device(Base): def __init__(self): super(Device, self).__init__() self.offset = (0, 0) # 记录本次位置偏移量 def move(self, x, y): self.offset = (self.postion[0] - x, self.postion[1] - y) super(Device, self).move(x, y) def get_offset(self...