首頁  >  文章  >  後端開發  >  Python物件導向之類和實例

Python物件導向之類和實例

不言
不言原創
2018-04-14 10:16:331736瀏覽

本篇文章給大家分享的內容是關於Python物件導向之類和實例,有著一定的參考價值,有需要的朋友可以參考一下

類別和實例


物件導向最重要的概念就是類別(Class)和實例(Instance),必須牢記類別是抽象的模板,例如Student類,而實例是根據類別創建出來的一個個具體的“物件”,每個物件都擁有相同的方法,但各自的資料可能不同。

仍以Student類別為例,在Python中,定義類別是透過class關鍵字:

class Student(object):
   pass

class後面緊跟著是類別名,即Student,類別名稱通常是大寫開頭的單詞,緊跟著是(object),表示該類是從哪個類繼承下來的,繼承的概念我們後續再講,通常,如果沒有合適的繼承類,就使用object類,這是所有類最終都會繼承的類別。

定義好了Student類,就可以根據Student類別建立出Student的實例,而建立實例是透過類別名稱()實現的:

class Student(object):
   pass

bart = Student()
print(bart)
print(Student)
<__main__.Student object at 0x000001A9412A47B8>
<class &#39;__main__.Student&#39;>

可以看到,變數bart指向的就是一個Student的實例,後面的0x000001A9412A47B8是記憶體位址,每個object的位址都不一樣,而Student本身則是一個類別。

可以自由地給一個實例變數綁定屬性,例如,給實例bart綁定一個name屬性:

class Student(object):
    def __init__(self,name,score):
        self.name = name
        self.score = score
    def print_score(self):
        print(&#39;%s:%s&#39;%(self.name,self.score))

bart = Student(&#39;Boyuan Zhou&#39;,100)
lisa = Student(&#39;Maomao&#39;,100)
bart.name= &#39;jeff&#39;
print(bart.name)
jeff

由於類別可以起到模板的作用,因此,可以在創建實例的時候,把一些我們認為必須綁定的屬性強制填寫進去。透過定義一個特殊的__init__方法,在創建實例的時候,就把name,score等屬性綁上去:

class Student(object):
    def __init__(self,name,score):
        self.name = name
        self.score = score

注意到__init__方法的第一個參數永遠都是self,表示所建立的實例本身,因此,在__init__方法內部,就可以把各種屬性綁定到self,因為self就指向創建的實例本身。

有了__init__方法,在創建實例的時候,就不能傳入空的參數了,必須傳入與__init__方法匹配的參數,但self不需要傳,Python解釋器自己會把實例變數傳進去:

class Student(object):
    def __init__(self,name,score):
        self.name = name
        self.score = score
    def print_score(self):
        print(&#39;%s:%s&#39;%(self.name,self.score))

bart = Student(&#39;Boyuan Zhou&#39;,100)
lisa = Student(&#39;Maomao&#39;,100)
bart.name= &#39;jeff&#39;
print(bart.name)
print(bart.score)
jeff
100

和普通的函數相比,在類別中定義的函數只有一點不同,就是第一個參數永遠是實例變數self,而且,當呼叫時,不用傳遞該參數。除此之外,類別的方法和普通函數沒有差別,所以,你還是可以用預設參數、可變參數、關鍵字參數、和命名關鍵字參數。

資料封裝

物件導向程式設計的一個重要特點是資料封裝。在上面的Student類別中,每個實例就擁有各自的name和score這些資料。我們可以透過函數來存取這些數據,例如列印一個學生的成績

>>> std1 = {&#39;name&#39;:&#39;jeff&#39;,&#39;score&#39;:100}
>>> std2 = {&#39;name&#39;:&#39;xin&#39;,&#39;score&#39;:0}
>>> def print_score(std):
...     print(&#39;%s:%s&#39;%(std[&#39;name&#39;],std[&#39;score&#39;]))
...
>>> print_score(std1)
jeff:100
>>> print_score(std2)
xin:0

但是,既然Student實例本身就擁有這些數據,要存取這些數據,就沒有必要從外面的函數去訪問,可以直接在Student類別的內部定義存取資料的函數,這樣,就把「資料」給封裝起來了。這些封裝資料的函數是和Student類別本身是關聯起來的,我們稱之為類別的方法:

class Student(object):
    def __init__(self,name,score):
        self.name = name
        self.score = score
    def print_score(self):
        print(&#39;%s:%s&#39;%(self.name,self.score))

要定義一個方法,除了第一個參數是self外,其他和普通函數一樣。要調用一個方法,只需要在實例變數上直接調用,除了self不用傳遞,其他參數正常傳入:

bart.print_score()
jeff:100

這樣一來,我們從外部看Student類,就只需要知道,創建實例需要給name和score,而如何列印,都是Student類別的內部定義的,這些資料和邏輯被「封裝」起來了,呼叫很容易,但卻不用知道內部實作的細節。

封裝的另一個好處是可以為Student類別增加新的方法,例如get_grade:

class Student(object):
    def __init__(self,name,score):
        self.name = name
        self.score = score
    def print_score(self):
        print(&#39;%s:%s&#39;%(self.name,self.score))
    def get_grade(self):
        if self.score >=90:
            return 'A'
        elif self.score>=600:
            return  'B'
        else:
            return  'C'
bart = Student('Boyuan Zhou',100)
lisa = Student('Maomao',100)
bart.name= 'jeff'
bart.print_score()

print(bart.get_grade())
jeff:100
A

相關推薦:

關於python物件導向範例程式碼


#

以上是Python物件導向之類和實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:PHP策略模式下一篇:PHP策略模式