Python类的继承用法示例


Posted in Python onJanuary 31, 2019

本文实例讲述了Python类的继承用法。分享给大家供大家参考,具体如下:

python —类的继承

root@kali:~/python/mod# vi class2.py
root@kali:~/python/mod# cat class2.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
class person:
  def __init__(self,name,nationality,age,sex,job):
    self.Name = name
    self.Country = nationality
    self.Age = age
    self.Sex= sex
    self.Job = job
  def talk(self,msg):
    self.msg = msg#定义msg变量,可以在python交换环境中调用
    if self.msg != 0:
      print '''Hello %s,
    you are from %s
    you are %s years old
    you are %s
    your job is %s.
    Am i right?''' %(self.Name,self.Country,self.Age,self.Sex,self.Job)
  def skill(self):
    if self.Job == 'IT Engineor':
      print '''To be IT Engineor,you must many skills:\n
    1.Linux
    2.shell
    3.database
    4.python
    '''
  def action(self):
    if self.msg != 2:
      print 'call this fuction!!'
    else:
      pass
class love(person):#继承父类peson信息,写恋爱史
  def __init__(self,number_of_friends,name,nationality,age,sex,job):
    person.__init__(self,name,nationality,age,sex,job)#继承父类信息
    self.number = number_of_friends
  def interaction(self):
    first_lover = raw_input("please input your first love's name:")
    first_age = raw_input("how oid are you?")
    status = raw_input("what is your current status:?")
    self.status = status
    self.talk(2)
    year_gap = int(self.Age) - int(first_age)
    print '''\nAnd your personal relationships as folloe:
    your met your first when you were %s,her name is %s
    now you are %s,but %s years later,now xx years past ,you still in love with her!you have % friends'''% (first_age,first_lover,status,year_gap,self.number)
#p = person('xuweibo','CN','19','Malse','IT Engineor')
p2 = love(2,'xuweibo','CN','76','Malse','IT Engineor')
p2.interaction()
'''
#--------绑定实例-------------
p = person('xuweibo','CN','19','Malse','IT Engineor')
p.talk(1)
p.skill()
p.action()
#----------------------------
#----------不绑定实例-------------------
#person('xuweibo','CN','19','Malse','IT Engineor').talk(2)
#person('xuweibo','CN','19','Malse','IT Engineor').skill()
#--------------------------------------
'''
root@kali:~/python/mod#

运行:

root@kali:~/python/mod#
root@kali:~/python/mod# python class2.py
please input your first love's name:cuihua
how oid are you?21
what is your current status:?sigle
Hello xuweibo,
    you are from CN
    you are 76 years old
    you are Malse
    your job is IT Engineor.
    Am i right?
And your personal relationships as folloe:
    your met your first when you were 21,her name is cuihua
    now you are sigle,but 55 years later,now xx years past ,you still in love with her!you have 2.000000riends
root@kali:~/python/mod#

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
python中os模块详解
Oct 14 Python
解决pandas使用read_csv()读取文件遇到的问题
Jun 15 Python
Linux系统(CentOS)下python2.7.10安装
Sep 26 Python
python自动发送测试报告邮件功能的实现
Jan 22 Python
完美解决Python matplotlib绘图时汉字显示不正常的问题
Jan 29 Python
对python判断ip是否可达的实例详解
Jan 31 Python
详解Python 4.0 预计推出的新功能
Jul 26 Python
详解Python中的format格式化函数的使用方法
Nov 20 Python
Python 剪绳子的多种思路实现(动态规划和贪心)
Feb 24 Python
Python中and和or如何使用
May 28 Python
python初步实现word2vec操作
Jun 09 Python
Pytorch中的学习率衰减及其用法详解
Jun 05 Python
判断python对象是否可调用的三种方式及其区别详解
Jan 31 #Python
python3使用QQ邮箱发送邮件
May 20 #Python
Python实现FTP弱口令扫描器的方法示例
Jan 31 #Python
对python条件表达式的四种实现方法小结
Jan 30 #Python
python从子线程中获得返回值的方法
Jan 30 #Python
学生信息管理系统Python面向对象版
Jan 30 #Python
python学生管理系统开发
Jan 30 #Python
You might like
php把数组值转换成键的方法
2015/07/13 PHP
php语言中使用json的技巧及json的实现代码详解
2015/10/27 PHP
PHP实现路由映射到指定控制器
2016/08/13 PHP
PHP多维数组排序array详解
2017/11/21 PHP
Ajax请求在数据量大的时候出现超时的解决方法
2014/02/27 Javascript
JS往数组中添加项性能分析
2015/02/25 Javascript
jQuery实现鼠标划过添加和删除class的方法
2015/06/26 Javascript
js实现文字向上轮播功能
2017/01/13 Javascript
Bootstrap Table 搜索框和查询功能
2017/11/30 Javascript
Three.js 再探 - 写一个微信跳一跳极简版游戏
2018/01/04 Javascript
JavaScript实现微信红包算法及问题解决方法
2018/04/26 Javascript
Angular8基础应用之表单及其验证
2019/08/11 Javascript
vue使用一些外部插件及样式的配置代码
2019/11/18 Javascript
JavaScript检测浏览器是否支持CSS变量代码实例
2020/04/03 Javascript
2020淘宝618理想生活列车自动领喵币js脚本的代码
2020/06/02 Javascript
[01:34]完美“圣”典宣传片震撼发布,12.17与你不见不散
2016/12/16 DOTA
学习python (2)
2006/10/31 Python
Python中asyncore的用法实例
2014/09/29 Python
对于Python异常处理慎用“except:pass”建议
2015/04/02 Python
Python 使用SMTP发送邮件的代码小结
2016/09/21 Python
spark: RDD与DataFrame之间的相互转换方法
2018/06/07 Python
在Python中Dataframe通过print输出多行时显示省略号的实例
2018/12/22 Python
PyCharm设置每行最大长度限制的方法
2019/01/16 Python
利用python实现汉字转拼音的2种方法
2019/08/12 Python
python 实现单例模式的5种方法
2020/09/23 Python
html5 web本地存储将取代我们的cookie
2012/12/26 HTML / CSS
天猫国际进口超市直营:官方直采,一站购齐
2017/12/11 全球购物
King Apparel官网:英国街头服饰品牌
2019/09/05 全球购物
linux面试题参考答案(3)
2012/09/13 面试题
美国旅游签证工作证明
2014/10/14 职场文书
考研经验交流会策划书
2015/11/02 职场文书
Python Pandas知识点之缺失值处理详解
2021/05/11 Python
redis内存空间效率问题的深入探究
2021/05/17 Redis
python如何利用traceback获取详细的异常信息
2021/06/05 Python
Python干货实战之八音符酱小游戏全过程详解
2021/10/24 Python
德劲DE1107指针试高灵敏度全波段收音机机评
2022/04/05 无线电