python 物件導向

來源:互聯網
上載者:User

標籤:建立   運行   @class   col   .com   子類   tin   icm   src   

類   屬性          執行個體變數:   __init__需要初始化的變數,執行個體變數範圍就是執行個體本身          類變數:      寫在class全域的變數          私人屬性 ,如下:這樣的話通過執行個體無法調用這個屬性 。          

          當要訪問私人屬性的時候,可以建立一個方法,內部方法是可以調用私人屬性的,然後通過調用這個方法來擷取私人屬性的值:

          

          print(r1.show_status())

 

          如果執行個體調用變數,執行個體變數和類變數都定義了相同變數,那麼優先去執行個體變數,沒有再找類變數。 

          

 

    方法                 構造方法:                def  __init__(self):                       self.name = name          解構函式,在執行個體釋放(刪除執行個體del 或者執行個體運行結束),銷毀的時候自動執行的,通常做一些收尾工作。如:關閉一些資料庫連結,關閉開啟的臨時檔案                def  __del__(self):                       do something           私人方法,定義普通方法的時候前面加兩個底線  對象       執行個體化一個類之後得到的對象 封裝      把一些功能的實現細節不對外暴露      第一個層面的封裝(什麼都不用做):建立類和對象會分別建立二者的名稱空間,我們只能用類名.或者obj.的方式去訪問裡面的名字,這本身就是一種封裝。      第二個層面的封裝:類中把某些屬性和方法隱藏起來(或者說定義成私人的),只在類的內部使用、外部無法訪問,或者留下少量介面(函數)供外部存取。  繼承      代碼重用      單繼承      多繼承                 2.7經典類(class People()),深度優先,新式類(class People(object)),廣度優先                 3.x廣度優先          繼承例子:
class Peolpe():    def __init__(self,name,age):        self.name = name        self.age = age    def talk(self):        print("%s is talking..."%self.name)    def sleep(self):        print("%s is sleeping..."%self.name)class Man(Peolpe): ##此處繼承    passm1 = Man("liyang",22)m1.sleep()

       

繼承重用

class Peolpe():    def __init__(self,name,age):        self.name = name        self.age = age    def talk(self):        print("%s is talking..."%self.name)    def sleep(self):        print("%s is sleeping..."%self.name)class Man(Peolpe): ##此處繼承    def eat(self):        print("%s is eating"%self.name)    def sleep(self):        Peolpe.sleep(self) ##此處調用父類方法,如果沒有這行,sleep方法會重寫,不會執行父類方法        print("man also is sleeping")m1 = Man("liyang",22)m1.sleep()

 

如果子類要新增初始化參數呢,要修改一下子類

class Man(People): ##此處繼承    def __init__(self,name,age,location):        People.__init__(self,name,age) ##繼承父類初始化參數        self.location = location  ##新加一個參數

或者

class Man(People): ##此處繼承    def __init__(self,name,age,location):        #People.__init__(self,name,age) ##繼承父類初始化參數        super(Man,self).__init__(name,age)  ##使用super內建函數,新式寫法        self.location = location  ##新加一個參數

 

多繼承:

# -*- coding:utf-8 -*-# Author:Brownyangyangclass People():    def __init__(self,name,age):        self.name = name        self.age = age    def talk(self):        print("%s is talking..."%self.name)    def sleep(self):        print("%s is sleeping..."%self.name)class Relation(object):    def make_friends(self,obj):        print("%s is makeing friends with %s"%(self.name,obj.name))        ##self.name是People那邊繼承過來的,obj 是定義的一個執行個體class Man(People,Relation): ##此處繼承    def __init__(self,name,age,location):        #People.__init__(self,name,age) ##繼承父類初始化參數        super(Man,self).__init__(name,age)        self.location = location  ##新加一個參數    def eat(self):        print("%s is eating"%self.name)    def sleep(self):        People.sleep(self) ##此處調用父類方法,如果沒有這行,sleep方法會重寫,不會執行父類方法        print("man also is sleeping in %s"%self.location)class Woman(People,Relation):    def get_birth(self):        print("%s is borning the baby"%self.name)m1 = Man("xiaohong",22,"nantong")# m1.sleep()w1 = Woman("xiaoming",32)# w1.get_birth()m1.make_friends(w1)

結果:xiaohong is makeing friends with xiaoming

 多態        介面重用,一種介面多次實現   方法修飾 靜態方法 @staticmethod        只是名義上歸類管,實際上跟類沒什麼關係了,實際上在靜態方法裡訪問不了類或執行個體中的任何屬性 類方法@classmethod       只能訪問類變數,不能訪問執行個體變數 屬性方法@property      把一個方法變成一個靜態屬性      

          

python 物件導向

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.