Python類的繼承用法示例
本文實(shí)例講述了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交換環(huán)境中調(diào)用
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()
'''
#--------綁定實(shí)例-------------
p = person('xuweibo','CN','19','Malse','IT Engineor')
p.talk(1)
p.skill()
p.action()
#----------------------------
#----------不綁定實(shí)例-------------------
#person('xuweibo','CN','19','Malse','IT Engineor').talk(2)
#person('xuweibo','CN','19','Malse','IT Engineor').skill()
#--------------------------------------
'''
root@kali:~/python/mod#
運(yùn)行:
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#
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python面向?qū)ο蟪绦蛟O(shè)計(jì)入門與進(jìn)階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- 基于python3 類的屬性、方法、封裝、繼承實(shí)例講解
- Python面向?qū)ο蟪绦蛟O(shè)計(jì)中類的定義、實(shí)例化、封裝及私有變量/方法詳解
- 基于python生成器封裝的協(xié)程類
- Python面向?qū)ο蟪绦蛟O(shè)計(jì)之類的定義與繼承簡(jiǎn)單示例
- Python類的繼承、多態(tài)及獲取對(duì)象信息操作詳解
- Python子類繼承父類構(gòu)造函數(shù)詳解
- Python面向?qū)ο箢惥帉懠?xì)節(jié)分析【類,方法,繼承,超類,接口等】
- Python面向?qū)ο蟪绦蛟O(shè)計(jì)OOP入門教程【類,實(shí)例,繼承,重載等】
- Python面向?qū)ο蟪绦蛟O(shè)計(jì)類的封裝與繼承用法示例
相關(guān)文章
Django結(jié)合使用Scrapy爬取數(shù)據(jù)入庫(kù)的方法示例
這篇文章主要介紹了Django結(jié)合使用Scrapy爬取數(shù)據(jù)入庫(kù)的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Django使用mysqlclient服務(wù)連接并寫入數(shù)據(jù)庫(kù)的操作過(guò)程
這篇文章主要介紹了Django使用mysqlclient服務(wù)連接并寫入數(shù)據(jù)庫(kù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
python之語(yǔ)句mode = 'test' if y&nb
這篇文章主要介紹了python之語(yǔ)句mode = 'test' if y is None else 'train'問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
Django數(shù)據(jù)映射(一對(duì)一,一對(duì)多,多對(duì)多)
本文主要介紹了Django數(shù)據(jù)映射(一對(duì)一,一對(duì)多,多對(duì)多),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Python批量處理工作簿和工作表的實(shí)現(xiàn)示例
本文主要介紹了使用Python批量處理工作簿和工作表,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
python運(yùn)用pygame庫(kù)實(shí)現(xiàn)雙人彈球小游戲
這篇文章主要為大家詳細(xì)介紹了python運(yùn)用pygame庫(kù)實(shí)現(xiàn)雙人彈球小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11

