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的父类和两个名为EmployeeDoctor的子类,我们继承了EmployeeDoctor类的Details类。 最后,创建EmployeeDoctor类的两个对象并进行设置,并使用它们的方法显示值。

Python代码演示带有两个子类的单继承示例 (Python code to demonstrate example of single inheritance with two child classes)

# Python code to demonstrate example of 
# single inheritance with two child classes

class Details:
    def __init__(self):
        self.__id=0
        self.__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 writer

Doctor 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 基类 派生类

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值