Refer to "Learn Python hard"
Website: https://learnpythonthehardway.org/book/ex41.html
-
-
Class X (Y)
-
-
"Make a class named X that Is-a Y."
-
-
create a class called X, Class X is Y (class X inherits Y), for example ' salmon ' is ' fish '
-
-
Class X (object): Def __init__ (self, J)
-
-
"Class X has-a __init__ that takes self and J parameters."
-
-
there is a class called __init__, which has two parameters, self, and J in __init__
.
-
-
__init__ is usually initialized, and in a function without a class, it starts with the first non-DEF statement
.
-
-
can be added to __init__ if there is something to run or initialize
-
-
Class X (object): Def M (self, J)
-
-
"Class X has-a function named M, takes self and J parameters."
-
-
a function called M is defined in the class, with the arguments of self and J in M, and the individual functions of the argument in the class must have.
-
-
foo = X ()
-
-
"Set Foo to an instance of class X."
-
-
instantiate class X with Foo
-
-
Foo. M (J)
-
-
"from Foo get the M function, and call it with parameters self, J."
-
-
get the function M from Foo, then call the function m, the argument in function M is J.
-
-
the Foo here must be instantiated after the class
-
-
Foo. K = Q
-
-
"from Foo get the K attribute and set it to Q."
-
-
get the attribute K from Foo and give it to K.
-
-
equivalent to Self.chose = TRUE or M.color = ' Blue '
Python Object-oriented classes