如何在Python中使用類的繼承?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
創(chuàng)新互聯(lián)建站堅持“要么做到,要么別承諾”的工作理念,服務領域包括:成都網(wǎng)站建設、網(wǎng)站設計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的萬寧網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!python是什么意思Python是一種跨平臺的、具有解釋性、編譯性、互動性和面向對象的腳本語言,其最初的設計是用于編寫自動化腳本,隨著版本的不斷更新和新功能的添加,常用于用于開發(fā)獨立的項目和大型項目。
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交換環(huán)境中調用 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#
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。