init(self)都可以用来调用父类的初始化方法,但super().init()是更推荐的方式。使用super().init()可以避免因父类初始化不正确而导致的问题,并且可以降低代码的复杂性和出错的可能性。在实际应用中,我们应该尽可能地使用super().init()来调用父类的初始化方法,除非有特殊的需求或理由需要使用xxxClass._init(self...
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__('Tom') #要将子类Child和self传递进去 #c = Child("init Child") d...
__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 Manager(Employee, Customer): def __init__(self, salary, favorite, address): super().__init__(salary) # super(Manager, self).__init__(salary) 作用同上, # super调用父类的方法,由于继承了两个类,super根据mro顺序来确定调用哪个父类,子类继承写在前面的类 # 使用未绑定的方式调用Customer...
def __init__(self, grade, *args): print("Student init start") self._grade = grade super().__init__(*args) print(__class__) # super(Human, self).__init__(*args) # 从Human开始起查找,但是不包括起点 IdCard.__init__(self, *args) # 这种方法也可以 ...
class Person: def __init__(self, newPersionName): # = newPersionName; #1.如果此处不写成 #那么此处的name,只是__init__函数中的局部临时变量name而已 #和全局中的name,没有半毛钱关系 name = newPersionName; #此处只是为了代码演示,而使用了局部变量name, ...
classA:def__init__(self):self.n=2defadd(self,m):print('self is {0} @A.add'.format(self))self.n+=mclassB(A):def__init__(self):self.n=3defadd(self,m):print('self is {0} @B.add'.format(self))super().add(m)self.n+=3 ...
def super(class_name, self): mro = self.__class__.mro() return mro[mro.index(class_name) + 1] #mro()用来获得类的继承顺序。 例如: class Base(object): def __init__(self): print 'Base create' class childA(Base): def __init__(self): ...
classinitial(object):def__init__(self):print('This print is from initial object')self.param=3deffunc(self):return1classnew(initial):def__init__(self):print('This print is from new object')print(self.func())super(new,self).__init__()print(self.param)self.param=4if__name__=='_...
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...