python中多重继承
多重继承 (Multiple inheritance )
When we have one child class and more than one parent classes then it is called multiple inheritance i.e. when a child class inherits from more than one parent class.
当我们有一个子类和一个以上的父类时,则称为多重继承,即当一个子类从多个父类继承时。
In this program, we have two parent classes Personel and Educational and one child class named Student and implementing multiple inheritance.
在此程序中,我们有两个父类Personnel和Educational ,一个子类名为Student并实现了多重继承 。
Python代码演示多重继承的示例 (Python code to demonstrate example of multiple inheritance)
# Python code to demonstrate example of
# multiple inheritance
class Personel:
def __init__(self):
self.__id=0
self.__name=""
self.__gender=""
def setPersonel(self):
self.__id=int(input("Enter Id: "))
self.__name = input("Enter Name: ")
self.__gender = input("Enter Gender: ")
def showPersonel(self):
print("Id: ",self.__id)
print("Name: ",self.__name)
print("Gender: ",self.__gender)
class Educational:
def __init__(self):
self.__stream=""
self.__year=""
def setEducational(self):
self.__stream=input("Enter Stream: ")
self.__year = input("Enter Year: ")
def showEducational(self):
print("Stream: ",self.__stream)
print("Year: ",self.__year)
class Student(Personel,Educational):
def __init__(self):
self.__address = ""
self.__contact = ""
def setStudent(self):
self.setPersonel()
self.__address = input("Enter Address: ")
self.__contact = input("Enter Contact: ")
self.setEducational()
def showStudent(self):
self.showPersonel()
print("Address: ",self.__address)
print("Contact: ",self.__contact)
self.showEducational()
def main():
s=Student()
s.setStudent()
s.showStudent()
if __name__=="__main__":main()
Output
输出量
Enter Id: 101
Enter Name: Prem Sharma
Enter Gender: Male
Enter Address: Nehru Place, New Delhi
Enter Contact: 0123456789
Enter Stream: Computer Science
Enter Year: 2010
Id: 101
Name: Prem Sharma
Gender: Male
Address: Nehru Place, New Delhi
Contact: 0123456789
Stream: Computer Science
Year: 2010
翻译自: https://www.includehelp.com/python/example-of-multiple-inheritance-in-python.aspx
python中多重继承