第一點:封裝
創(chuàng)新互聯(lián)公司長期為超過千家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為鳳岡企業(yè)提供專業(yè)的網(wǎng)站設(shè)計、網(wǎng)站建設(shè),鳳岡網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
隱藏對象的屬性和實現(xiàn)細節(jié),僅對外提供公共訪問方式,在Python中用雙下線開頭的方式將屬性設(shè)置成私有的。
擁有三個好處:將變化隔離,便于使用,提高復(fù)用性,提高安全性。
第二點:繼承
繼承是一種創(chuàng)建新類的方式,在Python中,新建的類可以繼承一個或多個父類,父類又被稱為基類或超類,新建的類稱為派生類或子類。即一個派生類繼承基類的字段和方法,繼承也允許把一個派生類的對象作為一個基類對象對待。
第三點:多態(tài)
一種事物的多種體現(xiàn)形式,函數(shù)的重寫其實就是多態(tài)的一種體現(xiàn)。Python中,多態(tài)指是父類的引用指向子類的對象。
實現(xiàn)多態(tài)的步驟:
1. 定義新的子類;
2. 重寫對應(yīng)的父類方法;
3. 使用子類的方法直接處理,不調(diào)用父類的方法;
多態(tài)的好處:
1. 增加了程序的靈活性;
2. 增加了程序的可擴展性。
class Set(object):
def __init__(self,data=None):
if data == None:
self.__data = []
else:
if not hasattr(data,'__iter__'):
#提供的數(shù)據(jù)不可以迭代,實例化失敗
raise Exception('必須提供可迭代的數(shù)據(jù)類型')
temp = []
for item in data:
#集合中的元素必須是可哈希
hash(item)
if not item in temp:
temp.append(item)
self.__data = temp
#析構(gòu)函數(shù)
def __del__(self):
del self.__data
#添加元素,要求元素必須可哈希
def add(self, other):
hash(other)
if other not in self.__data:
self.__data.append(other)
else:
print('元素已存在,操作被忽略')
#刪除元素
def remove(self,other):
if other in self.__data:
self.__data.remove(other)
print('刪除成功')
else:
print('元素不存在,刪除操作被忽略')
#隨機彈出并返回一個元素
def pop(self):
if not self.__dat:
print('集合已空,彈出操作被忽略')
return
import random
item = random.choice(self.__data)
self.__data.remove(item)
return item
#運算符重載,集合差集運算
def __sub__(self, other):
if not isinstance(other,Set):
raise Exception('類型錯誤')
#空集合
result = Set()
#如果一個元素屬于當前集合而不屬于另一個集合,添加
for item in self.__data:
if item not in other.__data:
result.__data.append(item)
return result
#提供方法,集合差集運算,復(fù)用上面的代碼
def difference(self,other):
return self - other
#|運算符重載,集合并集運算
def __or__(self, other):
if not isinstance(other,Set):
raise Exception('類型錯誤')
result = Set(self.__data)
for item in other.__data:
if item not in result.__data:
result.__data.append(item)
return result
#提供方法,集合并集運算
def union(self,otherSet):
return self | otherSet
#運算符重載,集合交集運算
def __and__(self, other):
if not isinstance(other,Set):
raise Exception('類型錯誤')
result = Set()
for item in self.__data:
if item in other.__data:
result.__data.append(item)
return result
#^運算符重載,集合對稱差集
def __xor__(self, other):
return (self-other) | (other-self)
#提供方法,集合對稱差集運算
def symetric_difference(self,other):
return self ^ other
#==運算符重載,判斷兩個集合是否相等
def __eq__(self, other):
if not isinstance(other,Set):
raise Exception('類型錯誤')
if sorted(self.__data) == sorted(other.__data):
return True
return False
#運算符重載,集合包含關(guān)系
def __gt__(self, other):
if not isinstance(other,Set):
raise Exception('類型錯誤')
if self != other:
flag1 = True
for item in self.__data:
if item not in other.__data:
#當前集合中有的元素不屬于另一個集合
flag1 = False
break
flag2 = True
for item in other.__data:
if item not in self.__data:
#另一集合中的元素不屬于當前集合
flag2 = False
break
if not flag1 and flag2:
return True
return False
#=運算符重載,集合包含關(guān)系
def __ge__(self, other):
if not isinstance(other,Set):
raise Exception('類型錯誤')
return self == other or self other
#提供方法,判斷當前集合是否為另一個集合的真子集
def issubset(self,other):
return selfother
#提供方法,判斷當前集合是否為另一集合的超集
def issuperset(self,other):
return self other
#提供方法,清空集合所有元素
def clear(self):
while self.__data:
del self.__data[-1]
print('集合已清空')
#運算符重載,使得集合可迭代
def __iter__(self):
return iter(self.__data)
#運算符重載,支持in運算符
def __contains__(self, item):
return item in self.__data
#支持內(nèi)置函數(shù)len()
def __len__(self):
return len(self.__data)
#直接查看該類對象時調(diào)用該函數(shù)
def __repr__(self):
return '{'+str(self.__data)[1:-1]+'}'
#使用print()函數(shù)輸出該類對象時調(diào)用該函數(shù)
__str__ = __repr__
Python 只有在排序Elements時才會加空格,其他時候并不會加。
notebook?name="math?notes"?date="2010"/
!--
排序后加空格。
--
notebook?date="2010"?name="math?notes"?/
這個基本是沒有一點關(guān)聯(lián)。。。只是名字容易混淆而已 重寫就是對父類的方法重寫,改變方法體中的語句。。。。 重載就是同一個函數(shù)名,參數(shù)個數(shù)、類型、排列順序不同,jvm根據(jù)參數(shù)來決定調(diào)用哪一個方法
分享文章:函數(shù)重寫python 函數(shù)重寫和函數(shù)重載有什么不同
文章來源:http://muchs.cn/article6/hgsoog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、電子商務(wù)、網(wǎng)站設(shè)計公司、動態(tài)網(wǎng)站、云服務(wù)器、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)