Python类的继承用法示例

yipeiwu_com5年前Python基础

本文实例讲述了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数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》及《Python入门与进阶经典教程

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

相关文章

python3实现表白神器

本文实例为大家分享了使用python3制作的表白神器,供大家参考,具体内容如下 使用python3制作的小玩意,哪里需要改造的,望各位评论一下! #coding=utf-8 #第一个...

关于Python-faker的函数效果一览

tags faker 随机 虚拟 faker文档链接 代码程序: # -*- coding=utf-8 -*- import sys from faker import Factor...

编写Python脚本来实现最简单的FTP下载的教程

访问FTP,无非两件事情:upload和download,最近在项目中需要从ftp下载大量文件,然后我就试着去实验自己的ftp操作类,如下(PS:此段有问题,别复制使用,可以参考去试验自...

解决python彩色螺旋线绘制引发的问题

解决python彩色螺旋线绘制引发的问题

彩色螺旋线的绘制代码如下: import turtle import time turtle.pensize(2) turtle.bgcolor('black') colors =...

Python与R语言的简要对比

Python与R语言的简要对比

数据挖掘技术日趋成熟和复杂,随着互联网发展以及大批海量数据的到来,之前传统的依靠spss、SAS等可视化工具实现数据挖掘建模已经越来越不能满足日常需求,依据美国对数据科学家(data s...