在解释super() 之前,我们首先需要了解多重继承的概念。
多重继承:意味着一个子类可以继承多个父类。
在以下示例中,子类从父类继承了属性方法。
class Father:
   fathername = ""
   def father(self):
   print(self.fathername)
class Mother:
   mothername = ""
   def mother(self):
   print(self.mothername)
class Child(Father, Mother):
   def parent(self):
   print("Father :", self.fathername)
   print("Mother :", self.mothername)
s1 = Child()s1.fathername = "Srinivas"
s1.mothername = "Anjali"
s1.parent()输出结果
Father : Srinivas Mother : Anjali
在下面的示例中,显示了(即) super()具有多个继承
super() :super函数可用于替换对的显式调用
class Father:
   fathername = ""
   def father(self):
   print(self.fathername)
class Mother:
   mothername = ""
   def mother(self):
   print(self.mothername)
class Child(Father, Mother):
   def parent(self):
   super().__init__()
   print("i am here")
   print("Father :", self.fathername)
   print("Mother :", self.mothername)
s1 = Child()s1.fathername = "Srinivas"
s1.mothername = "Anjali"
s1.parent()当您运行程序时,输出将是
输出结果
i am here Father : Srinivas Mother : Anjali