Python中的单继承示例

In this program, we have a parent class named Details and child class named Employee, we are inheriting the class Details on the class Employee. And, finally creating an object of Employee class and by calling the method setEmployee() – we are assigning the values to the class variables and printing the values using showEmployee() function.

在这个程序中,我们有一个名为详情父类和子类名为Employee,我们在Employee类继承的类的细节 。 最后,创建一个Employee类的对象并调用setEmployee()方法–我们将这些值分配给类变量,并使用showEmployee()函数打印这些值。

Python代码演示单一继承的示例 (Python code to demonstrate example of single inheritance)

# Python code to demonstrate example of 
# single inheritance

class Details:
    def __init__(self):
        self.__id="<No Id>"
        self.__name="<No Name>"
        self.__gender="<No Gender>"
    def setData(self,id,name,gender):
        self.__id=id
        self.__name=name
        self.__gender=gender
    def showData(self):
        print("Id\t\t:",self.__id)
        print("Name\t\t:", self.__name)
        print("Gender\t\t:", self.__gender)

class Employee(Details): #Inheritance
    def __init__(self):
        self.__company="<No Company>"
        self.__dept="<No Dept>"
    def setEmployee(self,id,name,gender,comp,dept):
        self.setData(id,name,gender)
        self.__company=comp
        self.__dept=dept
    def showEmployee(self):
        self.showData()
        print("Company\t\t:", self.__company)
        print("Department\t:", self.__dept)


def main():
    e=Employee()
    e.setEmployee(101,"Prem Sharma","Male","New Delhi",110065)
    e.showEmployee()

if __name__=="__main__":
    main()

Output

输出量

Id              : 101
Name            : Prem Sharma
Gender          : Male
Company         : New Delhi
Department      : 110065


翻译自: https://www.includehelp.com/python/example-of-single-inheritance-in-python.aspx

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值