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è)父類方法
父類方法重寫!?。。。。。。。。。。。。。。。。。。?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
運(yùn)行結(jié)果:
Vector (23,25)
示例5:
'''#example 5 private class'''
class JustCounter(object):
"""docstring for JustCounter"""
__secretCount = 0 #私有變量
publicCount = 0 #公開(kāi)變量
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ū)ο笾涌凇⒊橄箢惻c多態(tài)詳解
- Python面向?qū)ο笾o態(tài)屬性、類方法與靜態(tài)方法分析
- Python面向?qū)ο笾瓷?自省機(jī)制實(shí)例分析
- Python面向?qū)ο蟪绦蛟O(shè)計(jì)之繼承與多繼承用法分析
相關(guān)文章
使用Python實(shí)現(xiàn)提取PDF文件中指定頁(yè)面的內(nèi)容
在日常工作和學(xué)習(xí)中,我們經(jīng)常需要從PDF文件中提取特定頁(yè)面的內(nèi)容,本文主要為大家詳細(xì)介紹了如何使用Python編程語(yǔ)言和兩個(gè)強(qiáng)大的庫(kù)——pymupdf和wxPython來(lái)實(shí)現(xiàn)這個(gè)任務(wù),需要的可以了解下2023-12-12
Python數(shù)據(jù)分析之彩票的歷史數(shù)據(jù)
這篇文章主要介紹了Python數(shù)據(jù)分析之彩票的歷史數(shù)據(jù),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
PyQt5 QTableView設(shè)置某一列不可編輯的方法
今天小編就為大家分享一篇PyQt5 QTableView設(shè)置某一列不可編輯的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
用python實(shí)現(xiàn)將數(shù)組元素按從小到大的順序排列方法
今天小編就為大家分享一篇用python實(shí)現(xiàn)將數(shù)組元素按從小到大的順序排列方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Python構(gòu)建一個(gè)文檔掃描器的實(shí)現(xiàn)
本文主要介紹了Python構(gòu)建一個(gè)文檔掃描器的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
pyinstaller 3.6版本通過(guò)pip安裝失敗的解決辦法(推薦)
這篇文章主要介紹了pyinstaller 3.6版本通過(guò)pip安裝失敗的解決辦法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
Python實(shí)現(xiàn)滑塊拼圖驗(yàn)證碼詳解
驗(yàn)證碼作為一種自然人的機(jī)器人的判別工具,被廣泛的用于各種防止程序做自動(dòng)化的場(chǎng)景中。傳統(tǒng)的字符型驗(yàn)證安全性已經(jīng)名存實(shí)亡的情況下,各種新型的驗(yàn)證碼如雨后春筍般涌現(xiàn),今天給大家分享一篇Python實(shí)現(xiàn)滑塊驗(yàn)證碼2022-05-05

