Python類的繼承用法示例
本文實例講述了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()
'''
#--------綁定實例-------------
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#
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python面向?qū)ο蟪绦蛟O計入門與進階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設計有所幫助。
相關(guān)文章
Python configparser模塊配置文件解析與應用探究
在Python中,configparser模塊是用于處理配置文件的重要工具,本文將全面探討configparser模塊的使用方法,包括讀取、修改、寫入配置文件,以及如何在實際項目中應用該模塊,結(jié)合豐富的示例代碼,將深入剖析該模塊的功能和靈活性2024-01-01
Python使用PyQt5實現(xiàn)與DeepSeek聊天的圖形化小軟件
在?PyQt5?中,菜單欄(QMenuBar)、工具欄(QToolBar)和狀態(tài)欄(QStatusBar)是?QMainWindow?提供的標準控件,用于幫助用戶更好地與應用程序交互,所以本文給大家介紹了Python使用PyQt5實現(xiàn)與DeepSeek聊天的圖形化小軟件,需要的朋友可以參考下2025-03-03
python爬蟲實現(xiàn)教程轉(zhuǎn)換成 PDF 電子書
本文給大家分享的是使用python爬蟲實現(xiàn)把《廖雪峰的 Python 教程》轉(zhuǎn)換成PDF的方法和代碼,有需要的小伙伴可以參考下2017-02-02
Selenium環(huán)境變量配置(火狐瀏覽器)及驗證實現(xiàn)
這篇文章主要介紹了Selenium環(huán)境變量配置(火狐瀏覽器)及驗證實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12
昨晚我用python幫隔壁小姐姐P證件照然后發(fā)現(xiàn)
大家好,我是Lex 喜歡欺負超人那個Lex 建議大家收藏哦,以后幫小姐姐P自拍,證件照,調(diào)尺寸,背景,摳圖,直接10行代碼搞定,瞬間高大上2021-08-08

