国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python類的繼承用法示例

2020-02-16 01:02:13
字體:
來源:轉載
供稿:網友

本文實例講述了Python類的繼承用法。分享給大家供大家參考,具體如下:

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?21what 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#

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python面向對象程序設計入門與進階教程》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結》及《Python入門與進階經典教程》

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彭水| 新津县| 资阳市| 康乐县| 白银市| 三门县| 桂东县| 海林市| 康保县| 铁力市| 荔浦县| 方山县| 武清区| 麟游县| 平安县| 茌平县| 突泉县| 同心县| 临湘市| 集贤县| 桂平市| 涿州市| 吉首市| 松江区| 增城市| 佛山市| 青田县| 元氏县| 彰武县| 临漳县| 泸西县| 荔波县| 桐梓县| 禹州市| 钟祥市| 天祝| 浮梁县| 息烽县| 东乡族自治县| 莲花县| 农安县|