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中利用sorted()函数排序的简单教程
Apr 27 Python
python中实现k-means聚类算法详解
Nov 11 Python
Python下使用Scrapy爬取网页内容的实例
May 21 Python
python中ASCII码字符与int之间的转换方法
Jul 09 Python
APIStar:一个专为Python3设计的API框架
Sep 26 Python
对Python中实现两个数的值交换的集中方法详解
Jan 11 Python
Python for循环与range函数的使用详解
Mar 23 Python
创建Django项目图文实例详解
Jun 06 Python
python  文件的基本操作 菜中菜功能的实例代码
Jul 17 Python
Python操作Sonqube API获取检测结果并打印过程解析
Nov 27 Python
python环境搭建和pycharm的安装配置及汉化详细教程(零基础小白版)
Aug 19 Python
opencv实现图像几何变换
Mar 24 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 编写的 25个游戏脚本
2009/05/11 PHP
PHP中使用mktime获取时间戳的一个黑色幽默分析
2012/05/31 PHP
php获取apk包信息的方法
2014/08/15 PHP
PHP 7.0.2 正式版发布
2016/01/08 PHP
phpMyAdmin无法登陆的解决方法
2017/04/27 PHP
使用SMB共享来绕过php远程文件包含的限制执行RFI的利用
2019/05/31 PHP
在textarea文本域中显示HTML代码的方法
2007/03/06 Javascript
使用IE的地址栏来辅助调试Web页脚本
2007/03/08 Javascript
Js从头学起(基本数据类型和引用类型的参数传递详细分析)
2012/02/16 Javascript
页面只能打开一次Cooike如何实现
2012/12/04 Javascript
js展开闭合效果演示代码
2013/07/24 Javascript
js获取html文件的思路及示例
2013/09/17 Javascript
JavaScript实现MIPS乘法模拟的方法
2015/04/17 Javascript
简述JavaScript的正则表达式中test()方法的使用
2015/06/16 Javascript
jQuery验证插件validate使用方法详解
2020/09/13 Javascript
JavaScript实现256色转灰度图
2017/02/22 Javascript
深入理解JavaScript继承的多种方式和优缺点
2017/05/12 Javascript
vue resource post请求时遇到的坑
2017/10/19 Javascript
详解Vue 事件修饰符capture 的使用
2017/12/29 Javascript
用Vue.js在浏览器中实现裁剪图像功能
2019/06/18 Javascript
Postman环境变量全局变量使用方法详解
2020/08/13 Javascript
python代码制作configure文件示例
2014/07/28 Python
tf.truncated_normal与tf.random_normal的详细用法
2018/03/05 Python
解决python中用matplotlib画多幅图时出现图形部分重叠的问题
2019/07/07 Python
python中的列表和元组区别分析
2020/12/30 Python
div或img图片高度随宽度自适应的方法
2020/02/06 HTML / CSS
荷兰皇家航空公司中国官网:KLM中国
2017/12/13 全球购物
英国奢侈品在线精品店:Hervia
2020/09/03 全球购物
战略合作协议书范本
2014/04/18 职场文书
党员干部公开承诺书范文
2015/04/27 职场文书
幼儿园教师读书笔记
2015/06/29 职场文书
2019年大学毕业生个人自我鉴定范文大全
2019/03/21 职场文书
golang http使用踩过的坑与填坑指南
2021/04/27 Golang
javascript条件式访问属性和箭头函数介绍
2021/11/17 Javascript
MySQL为数据表建立索引的原则详解
2022/03/03 MySQL
JS前端宏任务微任务及Event Loop使用详解
2022/07/23 Javascript