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编程模板汇总
Feb 12 Python
用Python实现命令行闹钟脚本实例
Sep 05 Python
Python中元组,列表,字典的区别
May 21 Python
python使用正则表达式的search()函数实现指定位置搜索功能
Nov 10 Python
PyTorch快速搭建神经网络及其保存提取方法详解
Apr 28 Python
python实现自动登录
Sep 17 Python
使用python telnetlib批量备份交换机配置的方法
Jul 25 Python
pytorch 使用单个GPU与多个GPU进行训练与测试的方法
Aug 19 Python
通过实例简单了解Python中yield的作用
Dec 11 Python
Tensorflow训练MNIST手写数字识别模型
Feb 13 Python
Python下opencv库的安装过程及问题汇总
Jun 11 Python
OpenCV绘制圆端矩形的示例代码
Aug 30 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读取数据库信息的几种方法
2008/05/24 PHP
php 服务器调试 Zend Debugger 的安装教程
2009/09/25 PHP
php读取mysql中文数据出现乱码的解决方法
2013/08/16 PHP
Docker搭建自己的PHP开发环境
2018/02/24 PHP
js解析json读取List中的实体对象示例
2014/03/11 Javascript
简单纯js实现点击切换TAB标签实例
2015/08/23 Javascript
原生JS实现美图瀑布流布局赏析
2015/09/07 Javascript
javascript嵌套函数和在函数内调用外部函数的区别分析
2016/01/31 Javascript
原生JS实现匀速图片轮播动画
2016/10/18 Javascript
详谈jQuery Ajax(load,post,get,ajax)的用法
2017/03/02 Javascript
基于node.js之调试器详解
2017/08/22 Javascript
jQuery实现的页面遮罩层功能示例【测试可用】
2017/10/14 jQuery
使用vue-router切换页面时,获取上一页url以及当前页面url的方法
2019/05/06 Javascript
Vue 2.0 中依赖注入 provide/inject组合实战
2019/06/20 Javascript
vue - props 声明数组和对象操作
2020/07/30 Javascript
vue中提示$index is not defined错误的解决方式
2020/09/02 Javascript
解决Python安装时报缺少DLL问题【两种解决方法】
2019/07/15 Python
图解python全局变量与局部变量相关知识
2019/11/02 Python
Python调用shell cmd方法代码示例解析
2020/06/18 Python
树莓派4B安装Tensorflow的方法步骤
2020/07/16 Python
CSS3实现文字波浪线效果示例代码
2016/11/20 HTML / CSS
CSS3实现背景透明文字不透明的示例代码
2018/06/25 HTML / CSS
全球领先的各类汽车配件零售商:Advance Auto Parts
2016/08/26 全球购物
欧洲最大的笔和书写专家:The Pen Shop
2017/03/19 全球购物
俄罗斯第一家篮球店:StreetBall
2020/07/30 全球购物
幼儿园教研活动方案
2014/01/19 职场文书
护理专业学生职业生涯规划范文
2014/03/11 职场文书
大学生活自我评价
2014/04/09 职场文书
户外宣传策划方案
2014/05/25 职场文书
护理目标管理责任书
2014/07/25 职场文书
小学教师先进事迹材料
2014/12/15 职场文书
汤姆索亚历险记读书笔记
2015/06/29 职场文书
小区物业管理2015年度工作总结
2015/10/22 职场文书
文案策划岗位个人自我评价(范文)
2019/08/08 职场文书
Java并发编程必备之Future机制
2021/06/30 Java/Android
「地球外少年少女」BD发售宣传CM公开
2022/03/21 日漫