欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python类的继承用法示例

程序员文章站 2022-05-07 18:19:14
本文实例讲述了python类的继承用法。分享给大家供大家参考,具体如下: python —类的继承 root@kali:~/python/mod# vi cla...

本文实例讲述了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程序设计有所帮助。