python 基类 派生类_在Python中具有两个子(派生)类的继承示例
生活随笔
收集整理的這篇文章主要介紹了
python 基类 派生类_在Python中具有两个子(派生)类的继承示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
python 基類 派生類
In this program, we have a parent class named Details and two child classes named Employee and Doctor, we are inheritance the class Details on the classes Employee and Doctor. And, finally creating two objects of Employee and Doctor classes and setting, showing the values using their methods.
在此程序中,我們有一個名為Details的父類和兩個名為Employee和Doctor的子類,我們繼承了Employee和Doctor類的Details類。 最后,創建Employee和Doctor類的兩個對象并進行設置,并使用它們的方法顯示值。
Python代碼演示帶有兩個子類的單繼承示例 (Python code to demonstrate example of single inheritance with two child classes)
# Python code to demonstrate example of # single inheritance with two child classesclass Details:def __init__(self):self.__id=0self.__name=""self.__gender=""def setDetails(self):self.__id=int(input("Enter Id: "))self.__name=input("Enter Name: ")self.__gender=input("Enter gender: ")def showDetails(self):print("Id: ",self.__id)print("Name: ",self.__name)print("Gender: ",self.__gender)class Employee(Details):def __init__(self):self.__company=""self.__desig=""def setEmployee(self):self.setDetails()self.__company=input("Enter Compmany Name: ")self.__desig=input("Enter Designation: ")def showEmployee(self):self.showDetails()print("Company: ",self.__company)print("Designation: ",self.__desig)class Doctor(Details):def __init__(self):self.__hospital=""self.__dept=""def setDoctor(self):self.setDetails()self.__hospital=input("Enter Hospital Name: ")self.__dept=input("Enter Department: ")def showDoctor(self):self.showDetails()print("Hospital: ",self.__hospital)print("Department",self.__dept)def main():print("Employee Object: ")e = Employee()e.setEmployee()e.showEmployee()print("\nDoctor Object: ")d=Doctor()d.setDoctor()d.showDoctor()if __name__=="__main__":main()Output
輸出量
Employee Object: Enter Id: 101 Enter Name: Prem Sharma Enter gender: Male Enter Compmany Name: IncludeHelp Enter Designation: Technical writer Id: 101 Name: Prem Sharma Gender: Male Company: IncludeHelp Designation: Technical writerDoctor Object: Enter Id: 201 Enter Name: Amit Shukla Enter gender: Male Enter Hospital Name: APOLLO Enter Department: Doctor Id: 201 Name: Amit Shukla Gender: Male Hospital: APOLLO Department Doctor翻譯自: https://www.includehelp.com/python/example-of-inheritance-with-two-child-derived-classes-in-python.aspx
python 基類 派生類
總結
以上是生活随笔為你收集整理的python 基类 派生类_在Python中具有两个子(派生)类的继承示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python | 在列表中指定索引处添加
- 下一篇: 应梦框架9.0框架_.Net框架能力问题