成員變量
建立對象的過程稱之爲實例化,當一個對象被建立後,包含三個方面的特性對象聚丙屬性和方法,
句柄用於區分不一樣的對象,
對象的屬性和方法,與類中的成員變量和成員函數對應,
obj = MyClass()建立類的一個實例,擴號對象,經過對象來調用方法和屬性python
類的屬性按使用範圍分爲公有屬性和私有屬性類的屬性範圍,取決於屬性的名稱,
**共有屬性**---在內中和內外都可以調用的屬性
**私有屬性**---不能在內外貝類之外函數調用
定義方式:以"__"雙下劃線開始的成員變量就是私有屬性
能夠經過instance.__classname__attribute方式訪問,
內置屬性--由系統在定義類的時候默認添加的由先後雙下劃線構成,如__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參數表示執行對象自己this
#!/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()函數處理過的函數,能被類所調用,也能被對象所調用(是繼承的關係)。spa
靜態方法:至關於「全局函數」,能夠被類直接調用,能夠被全部實例化對象共享,經過staticmethod()定義靜態方法, 靜態方法沒有self參數 裝飾器: @classmethod() @staticmethod()
#!/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()
經過類方法類內的方法 ,不涉及的屬性和方法 不會被加載,節省內存,快。 code
#!/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()
所謂內部類,就是在類的內部定義的類,主要目的是爲了更好的抽象現實世界。
例子:
汽車是一個類,汽車的底盤輪胎也能夠抽象爲類,將其定義到汽車內中,而造成內部類,
更好的描述汽車類,由於底盤輪胎是汽車的一部分。對象
內部類實例化方法:繼承
方法1:直接使用外部類調用內部類 方法2:先對外部類進行實例化,而後再實例化內部類 out_name = outclass_name() in_name = out_name.inclass_name() in_name.method()
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有屬性 class Chinese(object): print("I am chinese") 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.Chinese()
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有屬性 class Chinese(object): name ="I am a Chinese." 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.Chinese() #外部類調用內部類 print jack.name #外部類調用內部類對象
另外一種方法,外部類調用內部類對象 #!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有屬性 class Chinese(object): name ="I am a Chinese." 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") ren = People() #實例化外部類 jack = ren.Chinese() #實例化內部類 print jack.name #打印內部類屬性 或 print People.Chinese.name print People.Chinese().name
str(self)
構造函數與析構函數
構造函數:內存
用於初始化類的內部狀態,Python提供的構造函數是__init__():
__init__():方法是可選的,若是不提供,python會給出一個默認的__init__方法。
析構函數:
用於釋放對象佔用的資源,python提供的析構函數是__del__():
__del__():也是可選的,若是不提供,則python會在後臺提供默認析構函數。
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有屬性 class Chinese(object): name ="I am a Chinese." def __str__(self): return "This is People class" 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") ren = People() #實例化外部類 print ren #默認執行__str__
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有屬性 class Chinese(object): name ="I am a Chinese." def __str__(self): return "This is People class" def __init__(self,c='white'): #類實例化時自動執行 self.color = c self.think() 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('green') ren = People() #實例化外部類 print ren.color #經過對象訪問屬性是初始化後的值 print People.color #經過類訪問仍是原來的值 [root@localhost 20180110]# python test1.py I am a black I am a thinker 30 black yellow
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有屬性 class Chinese(object): name ="I am a Chinese." def __str__(self): return "This is People class" def __init__(self,c='white'): #類實例化時自動執行 print ("initing...") self.color = c self.think() f = open('test.py') 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") def __del__(self): print ("del....") self.f.close() jack = People('green') ren = People() #實例化外部類 print ren.color #經過對象訪問屬性是初始化後的值 print People.color #經過類訪問仍是原來的值
Python採用垃圾回收機制來清理再也不使用的對象;python提供gc模塊釋放再也不使用的對象。
Python採用「引用計數」的算法方式來處理回收,即:固然某個對象在其做用域內再也不被其
他對象引用的時候,python就自動化清除對象。
gc模塊collect()能夠一次性收集全部待處理的對象(gc.collect)
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有屬性 class Chinese(object): name ="I am a Chinese." def __str__(self): return "This is People class" def __init__(self,c='white'): #類實例化時自動執行 print ("initing...") self.color = c self.think() f = open('test.py') 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") def __del__(self): print ("del....") self.f.close() print gc.collect() 若是是0是沒有回收的。 jack = People('green') ren = People() #實例化外部類 print ren.color #經過對象訪問屬性是初始化後的值 print People.color #經過類訪問仍是原來的值
類的繼承
繼承是面向對象的重要特性之一,
繼承關係繼承是相對兩個類而言的父子關係
子類繼承了父類的全部公有屬性和方法,
繼承,實現了代碼重用
使用繼承
繼承能夠重用已經存在的數據和行爲,減小代碼的重複編寫,
Python在類名後使用一對括號來表示繼承關係,括號中的即類爲父類
class Myclass(ParentClass),
若是父類定義了__init__方法,子類必須顯式調用父類的__init__方法,
ParentClass.__init__(self,[args...])
若是子類須要擴展父類的行爲,能夠添加__init__方法的參數.
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") class Chinese(People): pass cn = Chinese() print cn.color cn.think()
父類中有構造函數:
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' def __init__(self): print "Init..." self.dwell = 'Earth' def think(self): print "I am a %s " % self.color print ("I am a thinker") class Chinese(People): pass cn = Chinese() print cn.dwell cn.think()
參數大於兩個:
Super 函數
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' def __init__(self,c): print "Init..." self.dwell = 'Earth' def think(self): print "I am a %s " % self.color print ("I am a thinker") class Chinese(People): def __init__(self): People.__init__(self,'red') pass cn = Chinese()
class A(object): def __init__(self): print "enter A" print "leave A" class B(object): def __init__(self): print "enter B" super(B,self),__init__() print "leave B" b = B()
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' def __init__(self,c): print "Init..." self.dwell = 'Earth' def think(self): print "I am a %s " % self.color print ("I am a thinker") class Chinese(People): def __init__(self): super(Chinese,self).__init__('red') pass cn = Chinese() cn.think()
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' def __init__(self,c): print "Init..." self.dwell = 'Earth' def think(self): print "I am a %s " % self.color print ("I am a thinker") class Chinese(People): def __init__(self): super(Chinese,self).__init__('red') def talk(self): print "I like taking." cn = Chinese() cn.think() cn.talk()
多重繼承
Python支持多重繼承,第一個類能夠繼承多個父類
語法:
class class_name(Parent_c1,Parent_c2,...)
注意:
當父類中出現多個自定義的__init__的方法時,
多重繼承,只執行第一個累的__init_方法,其餘不執行。
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' def __init__(self): print "Init..." self.dwell = 'Earth' def think(self): print "I am a %s " % self.color print ("My home is %s ") % self.dwell class Martian(object): color = 'red' def __init__(self): self.dwell = 'Martian' class Chinese(People,Martian): def __init__(self): People.__init__(self) cn = Chinese() cn.think()
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): def __init__(self): self.dwell = 'Earth' self.color = 'yellow' def think(self): print "I am a %s " % self.color print ("My home is %s ") % self.dwell class Martian(object): color = 'red' def __init__(self): self.dwell = 'Martian' def talk(self): print "I like talking" class Chinese(Martian,People): def __init__(self): People.__init__(self) cn = Chinese() cn.think() cn.talk()
類屬性,也是公有屬性,
類的私有屬性,
對象的共有屬性,
對象的私有屬性,
內置屬性,
函數的局部變量,
全局變量,
#/usr/bin/env python # -*- coding:utf-8 -*- class MyClass(object): var1 = '類屬性,類的公有屬性 var1' __var2 = '類的私有屬性 __var2' def func1(self): self.var3 = '對象的公有屬性 var3' self.__var4 = '對象的私有屬性 __var4' var5 = '函數的局部變量' mc = MyClass() mc.func1() #調用後才測打印出var3 print mc.var1 print mc._MyClass__var2 print mc.var3 mc1 = MyClass() # mc1.func1() #mc1沒有調用方法 print mc1.var3
經過類訪問:
#/usr/bin/env python # -*- coding:utf-8 -*- # @time :2018/1/2 21:06 # @Author :FengXiaoqing # @file :__init__.py.py # var6 = '全局變量 ' class MyClass(object): var1 = '類屬性,類的公有屬性 var1' ##定義在方法外 __var2 = '類的私有屬性 __var2' def func1(self): self.var3 = '對象的公有屬性 var3' ##定義在方法內 self.__var4 = '對象的私有屬性 __var4' var5 = '函數的局部變量' def func2(self): print self.var1 print self.__var2 print self.var3 print self.__var4 print self.var6 mc = MyClass() mc.func1() mc.func2() print '*'*50 print mc.__dict__ print MyClass.var1 #print MyClass.__var2 #不測經過類訪問 print mc.var3 #對象的屬性只能經過對象來訪問 #print MyClass.__var4 print MyClass.__dict__
4.類的方法總結
公有方法
私有方法
類方法
靜態方法
內置方法
class MyClass(object): name = 'Test' def func1(self): print self.name, print "我是公有方法." self.__func2() #func1間接調用了func2的私有方法 def __func2(self): print self.name, print "我是私有方法." def classFun(self): print self.name, print "我是類方法." def staticFun(self): print s.name, print "我是靜態方法." mc = MyClass() mc.func1()
@classmethod def classFun(self): print self.name, print "我是類方法." def staticFun(self): print s.name, print "我是靜態方法." mc = MyClass() mc.func1() MyClass.classFun()
用類方法:用裝飾器
調用靜態方法:
@staticmethod def staticFun(): print MyClass.name, print "我是靜態方法." mc = MyClass() mc.func1() MyClass.classFun() MyClass.staticFun()
調用內置方法:
class MyClass(object): name = 'Test' def __init__(self): self.func1() self.__func2() self.classFun() self.staticFun() def func1(self): print self.name, print "我是公有方法." def __func2(self): print self.name, print "我是私有方法." @classmethod def classFun(self): print self.name, print "我是類方法." @staticmethod def staticFun(): print MyClass.name, print "我是靜態方法." mc = MyClass()