python ---类的继承

来源:互联网 发布:软件构架实践 epub 编辑:程序博客网 时间:2024/06/06 07:22

python —类的继承

root@kali:~/python/mod# vi class2.pyroot@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:            passclass 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.pyplease input your first love's name:cuihuahow oid are you?21 what is your current status:?sigleHello 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.000000riendsroot@kali:~/python/mod# 
原创粉丝点击