python object-oriented : Inherit super() The use of
super() Usage of One :
class A:
def __init__(self):
self.a = ' This is an attribute '
def add(self, x):
y = x + 1
print(y)
class B(A):
def __init__(self):
super().__init__() # Both ways are possible , You can only choose one
A.__init__(self) # Both ways are possible , You can only choose one
def add(self, x):
super().add(x)
b = B()
b.add(2) # Output 3
super() Usage of Two :
class A ():
def __init__(self, a, b):
self.a = a
self.b = b
class B (A):
def __init__(self, a ,b):
super(D, self).__init__(a, b) # All three can be , You can only choose one , init No more in it self
super().__init__(a, b) # All three can be , You can only choose one , init No more in it self
A.__init__(self, a, b) # All three can be , You can only choose one , init No more in it self
super() Usage of 3、 ... and :
class A ():
def __init__(self, a, b):
self.a = a
self.b = b
class B (A):
def __init__(self, a, b, c, d ):
A.__init__(self, a, b)
self.c = c
self.d = d
class C (A):
def __init__(self, a, b, c, d ):
super(C, self).__init__( a, b) # Either of these can be , You can only choose one , init No more in it self
super().__init__(a, b) # Either of these can be , You can only choose one , init No more in it self
self.c = c
self.d = d
a = A(1,2)
b = B(1,2,3,4)
c = C(1,2,3,4)