本文實例講述了Python面向?qū)ο笾惡蛯嵗梅ā7窒斫o大家供大家參考,具體如下:
類
雖然 Python 是解釋性語言,但是它是面向?qū)ο蟮模軌蜻M行對象編程。至于何為面向?qū)ο螅诖司筒辉斦f了。面向?qū)ο蟪绦蛟O計本身就很值得深入學習,如要了解,請參閱網(wǎng)上其他的資料。
面向?qū)ο笞钪匾母拍罹褪穷悾–lass)和實例(Instance),牢記 類 是抽象的模板,比如Student類,而實例是根據(jù)類創(chuàng)建出來的一個個具體的“對象”,每個對象都擁有相同的方法,但各自的數(shù)據(jù)可能不同。
以Student類為例,在Python中,定義類是通過
class
關鍵字:
注意 :Python 2.X 需要在類名后面加 (object)? 這邊 pass 語句表示空語句段。
class 后面緊接著是類名,即Student,類名通常是大寫開頭的單詞,緊接著是(object),表示該類是從哪個類繼承下來的,繼承的概念我們后面再講,通常,如果沒有合適的繼承類,就使用object類,這是所有類最終都會繼承的類。
class Student(object): pass
Python 3.X 則只需要類名,不需要加 (object)
class Student: pass
實例
創(chuàng)建實例是通過類名+()實現(xiàn)的(若 __init__ 無或僅有self);如定義好了Student類,就可以根據(jù)Student類創(chuàng)建出Student的實例,如下:
class Student(object): pass May = Student() # 新建May實例 print(May)
運行結(jié)果:
<__main__.Student object at 0x0000000001DCC400>
可以看到,變量May指向的就是一個Student的object,后面的0x006C4770是內(nèi)存地址,每個object的地址都不一樣,而Student本身則是一個類。
可以自由地給一個實例變量綁定屬性,比如,給實例 May 綁定一個 name 屬性,這個 name 屬性是實例 May 特有的,其他新建的實例是沒有 name 屬性的
class Student(object): pass May = Student() # 新建May實例 print(May) May.name = "May" # 給實例 May 綁定 name 屬性為 "May" print(May.name) Peter = Student() # 新建Peter實例 # print(Peter.name) # 報錯,因為Peter沒有Name屬性
那么,如果我們需要類必須綁定屬性,那如何定義呢?? 請參見下文。
__init__ 構造函數(shù)
由于類可以起到模板的作用,因此,可以在創(chuàng)建實例的時候,把一些我們認為必須綁定的屬性強制填寫進去。 (
注意
__init__
雙下劃線)
如對于Student類,我們定義 name 和 score 屬性(所有Sudent 都須有的屬性):
__init__
方法的第一個參數(shù)永遠是
self
,表示創(chuàng)建的實例本身,因此,在
__init__
方法內(nèi)部,就可以把各種屬性綁定到self,因為self就指向創(chuàng)建的實例本身。
有了
__init__
方法,在創(chuàng)建實例的時候,就不能傳入空的參數(shù)了,必須傳入與
__init__
方法匹配的參數(shù),但
self
不需要傳,Python解釋器自己會把實例變量傳進去:
class Student(object): def __init__(self, name, score): self.name = name self.score = score May = Student("May",90) # 須要提供兩個屬性 Peter = Student("Peter",85) print(May.name, May.score) print(Peter.name, Peter.score)
__del__ 析構函數(shù)
Just like the __init__ method, there is another special method __del__ which is called when an object is going to die i.e. it is no longer being used and is being returned to the computer system for reusing that piece of memory.
The __del__ method is run when the object is no longer in use and there is no guarantee when that method will be run. If you want to explicitly see it in action, we have to use the del statement which is what we have done here.
相對于 構造函數(shù) Python 也有類似 C++ 中的析構函數(shù)
__del__
, Python的垃圾回收過程與常用語言的不一樣,如果一定需要,最好需要使用del語句來激活。
私有變量
Note for C++/Java/C# Programmers
All class members (including the data members) are public and all the methods are virtual in Python.
One exception: If you use data members with names using the double underscore prefix such as __privatevar, Python uses name-mangling to effectively make it a private variable.
Thus, the convention followed is that any variable that is to be used only within the class or object should begin with an underscore and all other names are public and can be used by other classes/objects. Remember that this is only a convention and is not enforced by Python (except for the double underscore prefix).
Python 中定義私有變量,命名規(guī)則為前綴加兩個下劃線 “__” ,注意不可前后都包含__XXX__(該命名表示為類屬性或內(nèi)建變量);還有一種命名為單下劃線 _XXX ,表示約定俗成不可訪問的變量。
class Person(object): def __init__(self,name,sex): self.name = name self.__sex = sex # sex 定義為私有變量 def print_title(self): if self.sex == "male": print("man") elif self.sex == "female": print("woman") May = Person("May","female") print(May.name) print(May.__sex) # 會報錯
更多關于Python相關內(nèi)容感興趣的讀者可查看本站專題:《Python面向?qū)ο蟪绦蛟O計入門與進階教程》、《Python數(shù)據(jù)結(jié)構與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設計有所幫助。
更多文章、技術交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
