Python面向?qū)ο蟪绦蛟O(shè)計示例小結(jié)
本文實例講述了Python面向?qū)ο蟪绦蛟O(shè)計。分享給大家供大家參考,具體如下:
示例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()
運行結(jié)果:
2
a value is 123
b value is 321
{'a': 123, 'b': 321}
示例2:
#encoding:utf-8 '''example 2''' class test: '這是一個測試的基類' def __init__(self,test): self.test = test '這是一個測試的基類' 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 '這是一個父類' def ParentsMethond(self): print '這是一個父類方法' 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 '這是一個子類' 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)
運行結(jié)果:
這是一個父類
這是一個子類
調(diào)用子類方法 child method
**********
這是一個父類方法
父類方法重寫?。。。。。。。。。。。。。。。。。。。?br /> **********
父類的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
運行結(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 #報錯,實例不能訪問私有變量 print counter._JustCounter__secreCount
感興趣的朋友可以測試上述代碼運行效果。
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python面向?qū)ο蟪绦蛟O(shè)計入門與進(jìn)階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
- Python面向?qū)ο笾惖亩x與繼承用法示例
- Python面向?qū)ο箢惥帉懠?xì)節(jié)分析【類,方法,繼承,超類,接口等】
- Python面向?qū)ο蟪绦蛟O(shè)計OOP深入分析【構(gòu)造函數(shù),組合類,工具類等】
- Python面向?qū)ο蟪绦蛟O(shè)計OOP入門教程【類,實例,繼承,重載等】
- Python面向?qū)ο笾惖膬?nèi)置attr屬性示例
- Python面向?qū)ο笾惡蛯ο髮傩缘脑鰟h改查操作示例
- Python面向?qū)ο笾涌?、抽象類與多態(tài)詳解
- Python面向?qū)ο笾o態(tài)屬性、類方法與靜態(tài)方法分析
- Python面向?qū)ο笾瓷?自省機(jī)制實例分析
- Python面向?qū)ο蟪绦蛟O(shè)計之繼承與多繼承用法分析
相關(guān)文章
使用Python實現(xiàn)提取PDF文件中指定頁面的內(nèi)容
在日常工作和學(xué)習(xí)中,我們經(jīng)常需要從PDF文件中提取特定頁面的內(nèi)容,本文主要為大家詳細(xì)介紹了如何使用Python編程語言和兩個強(qiáng)大的庫——pymupdf和wxPython來實現(xiàn)這個任務(wù),需要的可以了解下2023-12-12Python數(shù)據(jù)分析之彩票的歷史數(shù)據(jù)
這篇文章主要介紹了Python數(shù)據(jù)分析之彩票的歷史數(shù)據(jù),文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04PyQt5 QTableView設(shè)置某一列不可編輯的方法
今天小編就為大家分享一篇PyQt5 QTableView設(shè)置某一列不可編輯的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06用python實現(xiàn)將數(shù)組元素按從小到大的順序排列方法
今天小編就為大家分享一篇用python實現(xiàn)將數(shù)組元素按從小到大的順序排列方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07pyinstaller 3.6版本通過pip安裝失敗的解決辦法(推薦)
這篇文章主要介紹了pyinstaller 3.6版本通過pip安裝失敗的解決辦法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01