python 物件導向的類

來源:互聯網
上載者:User

標籤:self   ram   cti   attr   沒有   elf   tps   name   執行個體   

參考《learn python hard way》

  網址:https://learnpythonthehardway.org/book/ex41.html

class X(Y)
"Make a class named X that is-a Y."
建立一個叫x的類,類x是y(類x 繼承y),例如‘三文魚‘是‘魚’
class X(object): def __init__(self, J)
"class X has-a __init__ that takes self and J parameters."
類中有一個叫__init__,在__init__中有兩個參數self 和J
  一般都會有__init__就是初始化,在沒有類的函數中是從第一個非def的語句開始運行
  在如果有需要運行或是初始化的內容,可以加在__init__中
class X(object): def M(self, J)
"class X has-a function named M that takes self and J parameters."
類中定義了一個叫M的函數,M中有self和J的參數,self這個參數在類中的各個函數都必須有。
foo = X()
"Set foo to an instance of class X."
用foo來執行個體化類X
foo.M(J)
"From foo get the M function, and call it with parameters self, J."
從foo中獲得函數M,然後調用函數M,函數M中的參數是J。
  這裡的foo一定是執行個體化類後的
foo.K = Q
"From foo get the K attribute and set it to Q."
從foo中獲得屬性K,然後把它給K
  相當於self.chose = true或者M.color = ‘blue‘

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.