python中多重继承_Python中的多重继承范例

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中多重继承

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值