博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python中的单继承示例
阅读量:2531 次
发布时间:2019-05-11

本文共 1728 字,大约阅读时间需要 5 分钟。

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 inheritanceclass Details:    def __init__(self):        self.__id="
" self.__name="
" self.__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="
" self.__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              : 101Name            : Prem SharmaGender          : MaleCompany         : New DelhiDepartment      : 110065

翻译自:

转载地址:http://botzd.baihongyu.com/

你可能感兴趣的文章
day7
查看>>
iphone移动端踩坑
查看>>
vs无法加载项目
查看>>
Beanutils基本用法
查看>>
玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
查看>>
《BI那点儿事》数据流转换——百分比抽样、行抽样
查看>>
哈希(1) hash的基本知识回顾
查看>>
Leetcode 6——ZigZag Conversion
查看>>
dockerfile_nginx+PHP+mongo数据库_完美搭建
查看>>
Http协议的学习
查看>>
【转】轻松记住大端小端的含义(附对大端和小端的解释)
查看>>
设计模式那点事读书笔记(3)----建造者模式
查看>>
交换机划分Vlan配置
查看>>
yum安装Elasticsearch5.x
查看>>
正则表达式
查看>>
Python模块_json & pickle模块
查看>>
Python 模块之_os模块_os是与操作系统交互的接口
查看>>
通通玩blend美工(1)——荧光Button
查看>>
[UWP]了解模板化控件(8):ItemsControl
查看>>
使用JustDecompile修改程序集
查看>>