成員變量
對象的創建
創建對象的過程稱之為實例化,當一個對象被創建后,包含三個方面的特性對象聚丙屬性和方法,
句柄用于區分不同的對象,
對象的屬性和方法,與類中的成員變量和成員函數對應,
obj = MyClass()創建類的一個實例,擴號對象,通過對象來調用方法和屬性

類的屬性按使用范圍分為公有屬性和私有屬性類的屬性范圍,取決于屬性的名稱,
共有屬性---在內中和內外都能夠調用的屬性
私有屬性---不能在內外貝類以外函數調用
定義方式:以""雙下劃線開始的成員變量就是私有屬性
可以通過instance.classnameattribute方式訪問,
內置屬性--由系統在定義類的時候默認添加的由前后雙下劃線構成,如dic,module__
#!/usr/bin/env python
#-*- coding:utf-8 -*-
class People(object):
color = 'yellow'
__age = 30 #私有屬性
def think(self):
self.color = "black"
print "I am a %s " % self.color
print ("I am a thinker")
print self.__age
ren = People()
ren.color = '白色人'
print ren.color
ren.think()
print '#'*30
print("People.color")
print ren.__People__age ##測試時使用。如要調用 時,通過方法內調用 。成員函數
類的方法
方法的定義和函數一樣,但是需要self作為第一個參數.
類方法為:
公有方法
私有方法
類方法
靜態方法
公有方法:在類中和類外都都測調用的方法.
私有方法:不測被類的外部調用模塊,在方法前加個“__”c雙下劃線就是私有方法。
self參數:
用于區分函數和類的方法(必須有一個self)
self參數表示執行對象本身
#!/usr/bin/env python
#-*- coding:utf-8 -*-
class People(object):
color = 'yellow'
__age = 30 #私有屬性
def think(self):
self.color = "black"
print "I am a %s " % self.color
print ("I am a thinker")
print self.__age
def test(self):
self.think() # 內部調用
jack = People()
jack.test() #外部調用
#!/usr/bin/env python
#-*- coding:utf-8 -*-
class People(object):
color = 'yellow'
__age = 30 #私有屬性
def think(self):
self.color = "black"
print "I am a %s " % self.color
print ("I am a thinker")
print self.__age
def __talk(self):
print "I am talking with Tom"
def test(self):
self.__talk() # 內部調用talk()
jack = People()
jack.test() #外部調用類方法
類方法:被classmethod()函數處理過的函數,能被類所調用,也能被對象所調用(是繼承的關系)。
靜態方法:相當于“全局函數”,可以被類直接調用,可以被所有實例化對象共享,通過staticmethod()定義靜態方法, 靜態方法沒有self參數
裝飾器:@classmethod()
br/>@classmethod()
#!/usr/bin/env python
#-*- coding:utf-8 -*-
class People(object):
color = 'yellow'
__age = 30 #私有屬性
def think(self):
self.color = "black"
print "I am a %s " % self.color
print ("I am a thinker")
print self.__age
def __talk(self):
print "I am talking with Tom"
def test(self):
print 'Testing....'
cm = classmethod(test)
jack = People()
People.cm()
通過類方法類內的方法 ,不涉及的屬性和方法 不會被加載,節省內存,快。
#!/usr/bin/env python
#-*- coding:utf-8 -*-
class People(object):
color = 'yellow'
__age = 30 #私有屬性
def think(self):
self.color = "black"
print "I am a %s " % self.color
print ("I am a thinker")
print self.__age
def __talk(self):
print "I am talking with Tom"
def test(): ##沒有self 靜態調用 會把所有的屬性加載到內存里。
print People.__age # 通過類訪問內部變量
sm = staticmethod(test)
jack = People()
People.sm()裝飾調用類的方法:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
class People(object):
color = 'yellow'
__age = 30 #私有屬性
def think(self):
self.color = "black"
print "I am a %s " % self.color
print ("I am a thinker")
print self.__age
def __talk(self):
print "I am talking with Tom"
@classmethod #調用類的方法
def test(self):
print ("this is class method")
@staticmethod #調用類的方法
def test1():
print ("this is static method")
jack = People()
People.test()
People.test1()另外有需要云服務器可以了解下創新互聯cdcxhl.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
當前題目:Python中的類和方法使用舉例-創新互聯
網站網址:http://www.yijiale78.com/article16/dppsdg.html
成都網站建設公司_創新互聯,為您提供網頁設計公司、網站策劃、建站公司、網站排名、域名注冊、搜索引擎優化
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯