【循序渐进学Python】7.面向对象的核心——类型(上)
我們知道Python是一門面向?qū)ο蟮哪_本語言。從C#的角度來看:首先Python支持多繼承。Python 類型成員通常都是public的,并且所有成員函數(shù)都是virtual的(可以直接重寫)。
1. 定義類型
類是對象的模板,在Python中我們使用class關(guān)鍵字來定義一個類型。
1 # -- coding: utf-8 -- 2 class Employee(object): 3 # self 關(guān)鍵字就是對象自身的一個引用(類似于C#中的this關(guān)鍵字) 4 def setName(self,name): 5 self.name = name 6 7 def getName(self): 8 return self.name 9 10 def greet(self): 11 print "hello,world! I'm %s" % self.name?
可以這樣使用我們定義好的Employee類:
1 foo = Employee() 2 bob = Employee() 3 foo.setName('Sunshine') 4 bob.setName('Bob') 5 6 foo.greet() # hello,world! I'm Sunshine 7 bob.greet() # hello,world! I'm Bob 8 print foo.name # Sunshine 9 bob.name = 'Bob2' # name是Employee類的一個特性 10 bob.greet() # hello,world! I'm Bob2?
1.1 使用新式類型
在Python 2.2之后,對象的工作方式有了很大的改變,所有導(dǎo)致了在Python 2.x 版本中存在兩種形式的類:Python 2.2之前的舊式類,和之后新增的新式類,新式類提供了很多新的特性(比如:super函數(shù)、property函數(shù)等),如果不需要兼容舊版本的Python代碼那么做好使用新式類,在Python中聲明新式類有兩種方法:
1.把賦值語句__metaclass__ = type 放置在定義模塊的最開始位置,如下:
# -- coding: utf-8 -- _metaclass_ = type # 確定使用新式類class Employee():pass?
2.子類化object類型,或者是其他新式類型,如下:
class NewStyle(object):pass?
2. 特性和成員方法
對象包括特性和方法,特性只是作為對象的一部分的變量,成員方法則是存儲在對象內(nèi)部的函數(shù)。Python中的所有方法函數(shù)在聲明時顯式地將第一個參數(shù)(self)表示為對象(實例),這個參數(shù)的值在方法被調(diào)用時隱式賦值:
1 # -- coding: utf-8 -- 2 _metaclass_ = type # 確定使用新式類 3 4 class Class: 5 def method(self): 6 print 'I have a self' 7 8 def function(): 9 print 'i don\'t...' 10 11 instance = Class() 12 instance.method() # I have a self 13 instance.method = function # 綁定一個普通的函數(shù) 14 instance.method() # i don't...?
通過self參數(shù)可以使用該實例的所有成員對象:
1 # -- coding: utf-8 -- 2 _metaclass_ = type # 確定使用新式類 3 4 class Bird: 5 song = 'Squaawk!' 6 def sing(self): 7 print self.song 8 9 bird = Bird() 10 bird.sing() # Squaawk!?
2.1 私有變量和私有方法
Python其實并不存在不能訪問的私有變量和私有方法。不過在Python中有一個約定:以一個下劃線(_)開頭的名字,應(yīng)該作為一個非公共的API(不論是函數(shù)、方法或者數(shù)據(jù)成員)。我們可以這樣定義一個"私有變量":
_flag = True?
Python解釋器在遇到任何以雙下劃線(__)開頭的標識符將會被替換為_className__spam形式,其中className是當(dāng)前的類型名稱,__spam就是當(dāng)前的標識符名稱。所以我們可以這樣定義一個"私有方法":
1 # -- coding: utf-8 -- 2 _metaclass_ = type # 確定使用新式類 3 4 class Secretive: 5 def __inaccessible(self): 6 print 'Bet you can\' see me...' 7 8 def accessible(self): 9 print 'The secret message is:' 10 self.__inaccessible() 11 12 s = Secretive() 13 # AttributeError: Secretive instance has no attribute '__inaccessible' 14 s.__inaccessible()?
因為在類的內(nèi)部定義中,所有以雙下劃線開始的名字都被轉(zhuǎn)換成前面加上單下劃線和類名的形式,所以還是可以訪問私有方法:
# Bet you can' see me... s._Secretive__inaccessible()?
3. 繼承
作為面向?qū)ο笕齻€基本的特性之一———繼承,我們可以利用它在現(xiàn)有類型的基礎(chǔ)上創(chuàng)建自己的類型。在Python中定義派生類的方式如下:
1 # -- coding: utf-8 -- 2 _metaclass_ = type # 確定使用新式類 3 4 class Person: 5 def printName(self): 6 print 'Is a Person' 7 def printHello(self): 8 print 'hello,world' 9 10 class Employee(Person): 11 # override base Method 12 def printName(self): 13 print 'Is a Employee' 14 15 person = Person() 16 person.printName() # Is a Person 17 employee = Employee() 18 employee.printName() # Is a Employee 19 employee.printHello() # hello,world?可以看到子類繼承了基類的方法,由于在Python中所有的成員函數(shù)都是virtual的,所有我們也可以選擇重寫基類的方法。
?
3.1 多重繼承
Python是一個多繼承語言,使用多重繼承的語法很簡單,只需要在定義子類后面的括號中添加多個父類名稱即可,如:
class C(A,B):pass?
注意
多重繼承(multiple inheritance)是個很有用的工具。不過除非你特別熟悉多重繼承,否則應(yīng)該盡量避免使用。因為它可能會有很多出乎意料的行為。例如:一個方法從多個超類繼承,那么根據(jù)繼承的順序(class語句中):先繼承的類中的方法會重寫后繼承類中的方法。
?
3.2 查看繼承關(guān)系
Python中有兩個內(nèi)置函數(shù)可擁有查看繼承關(guān)系:
- 使用isinstance函數(shù)檢查實例的類型
- 使用issubclass函數(shù)檢查類的繼承關(guān)系
使用方式如下:
1 # -- coding: utf-8 -- 2 obj = "string" 3 print isinstance(obj,str) # True 4 print obj.__class__ # <type 'str'> 5 6 print issubclass(bool,int) # True 7 print bool.__bases__ # (<type 'int'>,)?
3.3 檢查對象中的方法是否存在
使用hasattr(x,'call')來判斷一個對象是否存在某個方法,如下:
1 # -- coding: utf-8 -- 2 _metaclass_ = type # 確定使用新式類 3 4 class Person: 5 def PrintName(self): 6 print 'Is a Person' 7 def PrintHello(self): 8 print 'hello,world' 9 10 per = Person() 11 12 # check method Exists 13 print hasattr(per,'PrintName') # True?
參考資料&進一步閱讀
深刻理解Python中的元類(metaclass)
Python基礎(chǔ)教程(第二版)
Python入門教程——類
轉(zhuǎn)載于:https://www.cnblogs.com/IPrograming/p/Python_object_1.html
總結(jié)
以上是生活随笔為你收集整理的【循序渐进学Python】7.面向对象的核心——类型(上)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#精华面试题及答案 三
- 下一篇: websocket python爬虫_p