Python面向?qū)ο蟪绦蛟O(shè)計(jì)示例小結(jié)
本文實(shí)例講述了Python面向?qū)ο蟪绦蛟O(shè)計(jì)。分享給大家供大家參考,具體如下:
示例1:
#encoding:utf-8
'''example 1
class test:
def __init__(self,year,**arg):
self.year = year
self.args = arg
def kaka(self):
if isinstance(self.year,str):
print 'input\'s year is a string! Error'
elif isinstance(self.year,int):
a = self.year%4
print a
else:
print 'Error!'
def deal_arg(self):
# for v in self.args:
# print '\n====================\n',v
for k in self.args:
print str(k)+'\tvalue is '+str(self.args[k])
print self.args
a = test(2014,a=123,b=321)
a.kaka()
a.deal_arg()
運(yùn)行結(jié)果:
2
a value is 123
b value is 321
{'a': 123, 'b': 321}
示例2:
#encoding:utf-8
'''example 2'''
class test:
'這是一個(gè)測(cè)試的基類'
def __init__(self,test):
self.test = test
'這是一個(gè)測(cè)試的基類'
print 'test.__doc__:',test.__doc__
print 'test.__name__:',test.__name__
print 'test.__module__:',test.__main__
print 'test.__bases__:',test.__bases__
print 'test.__dict__:',test.__dict__
示例3:
'''example 3 Class inheritance and method partial rewriting'''
class parent:
def __init__(self):
print '這是一個(gè)父類'
def ParentsMethond(self):
print '這是一個(gè)父類方法'
def Parenttest(self,arg):
self.arg = 'This is a test!'
print '父類的self變量: %s' %self.arg
parent.arg = arg
print '父類的變量: %s' %parent.arg
class child(parent):
"""docstring for child"""
def __init__(self):
print '這是一個(gè)子類'
def ChildMethod(self):
print '調(diào)用子類方法 child method'
def ParentsMethond(self):
print '父類方法重寫?。。。。。。。。。。。。。。。。。。。?
b= parent()
c = child()
c.ChildMethod()
print '*'*10
b.ParentsMethond()
c.ParentsMethond()
print '*'*10
c.Parenttest(3899)
運(yùn)行結(jié)果:
這是一個(gè)父類
這是一個(gè)子類
調(diào)用子類方法 child method
**********
這是一個(gè)父類方法
父類方法重寫?。。。。。。。。。。。。。。。。。。?!
**********
父類的self變量: This is a test!
父類的變量: 3899
示例4:
'''example 4 Operator overloading'''
class test:
def __init__(self,a,b):
self.a = a
self.b = b
def __str__(self):
return 'Vector (%d,%d)' % (self.a,self.b)
def __add__(self,other):
return test(self.a+other.a,self.b+other.b)
v1 = test(21,22)
v2 = test(2,3)
print v1 + v2
運(yùn)行結(jié)果:
Vector (23,25)
示例5:
'''#example 5 private class'''
class JustCounter(object):
"""docstring for JustCounter"""
__secretCount = 0 #私有變量
publicCount = 0 #公開變量
def count(self):
self.__secretCount +=1
self.publicCount +=1
print self.__secretCount
counter = JustCounter()
counter.count()
counter.count()
counter.count()
counter.count()
counter.count()
print counter.publicCount
print counter.__secretCount #報(bào)錯(cuò),實(shí)例不能訪問(wèn)私有變量
print counter._JustCounter__secreCount
感興趣的朋友可以測(cè)試上述代碼運(yùn)行效果。
更多關(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ì)有所幫助。
- Python面向?qū)ο笾惖亩x與繼承用法示例
- Python面向?qū)ο箢惥帉懠?xì)節(jié)分析【類,方法,繼承,超類,接口等】
- Python面向?qū)ο蟪绦蛟O(shè)計(jì)OOP深入分析【構(gòu)造函數(shù),組合類,工具類等】
- Python面向?qū)ο蟪绦蛟O(shè)計(jì)OOP入門教程【類,實(shí)例,繼承,重載等】
- Python面向?qū)ο笾惖膬?nèi)置attr屬性示例
- Python面向?qū)ο笾惡蛯?duì)象屬性的增刪改查操作示例
- Python面向?qū)ο笾涌?、抽象類與多態(tài)詳解
- Python面向?qū)ο笾o態(tài)屬性、類方法與靜態(tài)方法分析
- Python面向?qū)ο笾瓷?自省機(jī)制實(shí)例分析
- Python面向?qū)ο蟪绦蛟O(shè)計(jì)之繼承與多繼承用法分析
相關(guān)文章
執(zhí)行python腳本并傳入json數(shù)據(jù)格式參數(shù)方式
這篇文章主要介紹了執(zhí)行python腳本并傳入json數(shù)據(jù)格式參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
刪除DataFrame中值全為NaN或者包含有NaN的列或行方法
今天小編就為大家分享一篇?jiǎng)h除DataFrame中值全為NaN或者包含有NaN的列或行方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
Python使用re模塊正則提取字符串中括號(hào)內(nèi)的內(nèi)容示例
這篇文章主要介紹了Python使用re模塊正則提取字符串中括號(hào)內(nèi)的內(nèi)容,結(jié)合實(shí)例形式分析了Python使用re模塊進(jìn)行針對(duì)括號(hào)內(nèi)容的正則匹配操作,并簡(jiǎn)單解釋了相關(guān)修正符與正則語(yǔ)句的用法,需要的朋友可以參考下2018-06-06
PyTorch?Distributed?Data?Parallel使用詳解
這篇文章主要為大家介紹了PyTorch?Distributed?Data?Parallel使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
解決pycharm安裝后代碼區(qū)不能編輯的問(wèn)題
今天小編就為大家分享一篇解決pycharm安裝后代碼區(qū)不能編輯的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
PyTorch 遷移學(xué)習(xí)實(shí)戰(zhàn)
本文主要介紹了PyTorch 遷移學(xué)習(xí)實(shí)戰(zhàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
使用Python實(shí)現(xiàn)查找PDF中的指定文本并高亮顯示
在處理大量PDF文檔時(shí),有時(shí)我們需要快速找到特定的文本信息,本文將提供三個(gè)Python示例來(lái)幫助你在PDF文件中快速查找并高亮指定的文本,希望對(duì)大家有所幫助2024-03-03

