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 相关文章推荐
开源Web应用框架Django图文教程
Mar 09 Python
Python自动化开发学习之三级菜单制作
Jul 14 Python
python爬虫基本知识
Mar 05 Python
Python实现读取Properties配置文件的方法
Mar 29 Python
django 使用 request 获取浏览器发送的参数示例代码
Jun 11 Python
python调用摄像头显示图像的实例
Aug 03 Python
20行python代码的入门级小游戏的详解
May 05 Python
python3实现带多张图片、附件的邮件发送
Aug 10 Python
Pytorch损失函数nn.NLLLoss2d()用法说明
Jul 07 Python
Python实现像awk一样分割字符串
Sep 15 Python
pyspark对Mysql数据库进行读写的实现
Dec 30 Python
总结Python变量的相关知识
Jun 28 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中Y2K38的漏洞解决方法实例分析
2014/09/22 PHP
Laravel5.1数据库连接、创建数据库、创建model及创建控制器的方法
2016/03/29 PHP
php把文件设置为插件的技巧方法
2020/02/03 PHP
推荐dojo学习笔记
2007/03/24 Javascript
JavaScript加密解密7种方法总结分析
2007/10/07 Javascript
将string解析为json的几种方式小结
2010/11/11 Javascript
js防止表单重复提交实现代码
2012/09/05 Javascript
动态的创建一个元素createElement及删除一个元素
2014/01/24 Javascript
jquery列表拖动排列(由项目提取相当好用)
2014/06/17 Javascript
使用bootstrap validator的remote验证代码经验分享(推荐)
2016/09/21 Javascript
jQuery 插件封装的方法
2016/11/16 Javascript
详解webpack分离css单独打包
2017/06/21 Javascript
js实现图片粘贴上传到服务器并展示的实例
2017/11/08 Javascript
cnpm加速Angular项目创建的方法
2018/09/07 Javascript
vue侧边栏动态生成下级菜单的方法
2018/09/07 Javascript
AjaxFileUpload.js实现异步上传文件功能
2019/04/19 Javascript
jQuery事件委托代码实践详解
2019/06/21 jQuery
vue父子组件间引用之$parent、$children
2020/05/20 Javascript
解决vue.js中settimeout遇到的问题(时间参数短效果不稳定)
2020/07/21 Javascript
Vue-CLI 3 scp2自动部署项目至服务器的方法
2020/07/24 Javascript
Vue——解决报错 Computed property "****" was assigned to but it has no setter.
2020/12/19 Vue.js
Python中用memcached来减少数据库查询次数的教程
2015/04/07 Python
python统计文本文件内单词数量的方法
2015/05/30 Python
剖析Django中模版标签的解析与参数传递
2015/07/21 Python
python实现简单的文字识别
2018/11/27 Python
numpy中的ndarray方法和属性详解
2019/05/27 Python
Flask框架重定向,错误显示,Responses响应及Sessions会话操作示例
2019/08/01 Python
python如何写try语句
2020/07/14 Python
CSS3制作酷炫的三维相册效果
2016/07/01 HTML / CSS
html5组织内容_动力节点Java学院整理
2017/07/10 HTML / CSS
投标单位介绍信
2014/01/09 职场文书
2014卖家双十一活动策划书
2014/09/29 职场文书
刑事撤诉申请书
2015/05/18 职场文书
小学体育组工作总结2015
2015/07/21 职场文书
2016年国庆节新闻稿范文
2015/11/25 职场文书
Go中使用gjson来操作JSON数据的实现
2022/08/14 Golang