python學習筆記(六),python學習筆記

來源:互聯網
上載者:User

python學習筆記(六),python學習筆記

一. 類和執行個體

第一個物件導向程式:

class Student(object):    def __init__(self, name, score):        self.name = name        self.score = score    def print_score(self):        print '%s: %s' % (self.name, self.score)bart = Student('Bart Simpson', 59)lisa = Student('Lisa Simpson', 87)bart.print_score()lisa.print_score()
類名通常是大寫開頭的單詞,(object) 表示該類從哪個類繼承

__init__類似Java中的建構函式,對屬性進行初始化;參數self表示建立的執行個體本身,調用的時候self不需要傳,python解譯器自己會把執行個體變數傳進去。


二. 訪問限制

類執行個體的變數名以__開頭,就變成了一個私人變數(private),只有內部可以訪問,外部不能訪問

如果變數名以雙底線開頭,並且以雙底線結尾(__xxx__),它是特殊變數,特殊變數是可以直接存取的,表示private變數

如果變數名以一個底線開頭(_xxx),這樣的執行個體變數外部是可以訪問的,但是按照約定俗成的規定,當你看到這樣的變數時,意思就是,“雖然我可以被訪問,但是請把我看成私人變數,不要隨意訪問”


如何訪問私人變數?和Java一樣定義get、set方法:

class Student(object):    def __init__(self, score):        self.score = score    def get_name(self):        return self.__name    def set_score(self, score):        self.__score = score

三. 繼承和多態

繼承的好處:子類可以擷取父類的全部功能,當然子類也可以重寫父類的方法。

class Animal(object):    def run(self):        print 'Animal is running...'class Dog(Animal):    def run(self):        print 'Dog is running...'class Cat(Animal):    def run(self):        print 'Cat is running...'animal = Animal();animal.run() # Animal is running...dog = Dog() dog.run() # Dog is running...cat = Cat()cat.run() # Cat is running...        

多態:一個類的多種形態,比如:dog可以當成animal,cat也可以當成animal

class Animal(object):    def run(self):        print 'Animal is running...'            def run_twice(self, animal):        animal.run()class Dog(Animal):    def run(self):        print 'Dog is running...'class Cat(Animal):    def run(self):        print 'Cat is running...'animal = Animal()dog = Dog() cat = Cat()animal.run_twice(animal) # Animal is running...animal.run_twice(dog) # Dog is running...animal.run_twice(cat) # Cat is running...
多態的好處:當我們需要傳入Dog、cat的時候,我們只需要接受Animal類型就可以了,因為Dog、cat都是Animal類型,對於一個變數,我們只需要知道它是Animal類型,無需確切地知道它的子類型,就可以放心地調用run()方法,因此,傳入的任意類型,只要是Animal類或者子類,都會自動調用實際類型的run()方法,這就是多態。

四. 擷取對象資訊

判斷某個變數是否是某個類型可以用isinstance()

a = list() # a是list類型b = Animal() # b是Animal類型c = Dog() # c是Dog類型print isinstance(a, list) # Trueprint isinstance(b, Animal) # Trueprint isinstance(c, Dog) # True

判斷對象是什麼類型:

import typestype('abc')==types.StringType  # True
可以通過getattr(), setattr(), hasattr()來直接操作一個對象的狀態:

操作對象的屬性:

>>> hasattr(obj, 'x') # 有屬性'x'嗎?True>>> obj.x9>>> hasattr(obj, 'y') # 有屬性'y'嗎?False>>> setattr(obj, 'y', 19) # 設定一個屬性'y'>>> hasattr(obj, 'y') # 有屬性'y'嗎?True>>> getattr(obj, 'y') # 擷取屬性'y'19>>> obj.y # 擷取屬性'y'19
操作對象的方法:

hasattr(obj, 'power') # 有屬性'power'嗎?getattr(obj, 'power') # 擷取屬性'power'fn = getattr(obj, 'power') # 擷取屬性'power'並賦值到變數fn, fn指向obj.powerfn() # 調用fn()與調用obj.power()是一樣的

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.