標籤: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 物件導向的類