Python - Class and Subclass 学习笔记
Class
定义:A class is any type that is not built-in to Python. A value of this type is called object.
Class 和 Object 的 Folder
上方为object的folder (其中id number是object的地址),下方为class的folder,both folder are stored in heap space.
<object>.<name> 代表:
- 到object的folder中找attribute/method name
- If missing, check Class Folder
- If not in either, raise Error
class folder 内会存储:Data common to all objects
The Class Definition
定义:a blueprint for the objects of the class. A class defines the components of each object of the class. All objects of the class have the same components, meaning they have the same attributes and methods. The only difference between objects is the values of their attributes.
class <class-name> (<superclass>):
"class specification"
<getters and setters>
<initializer definition>
<method definition>
<assignment statement>
<other statement>
Constructor and Initializer
Constructor 定义: a function that creates a object for a class. It puts the object in heap space, and returns the name of the object (folder name) so you can store variable / attribute in it.
constructor 默认没有任何argument。生成一个empty folder。
特点:
- constructor 的 name 与 type of the object 一样(即class name)其中,instance is initially empty
- 创建一个 empty object folder
- It puts the folder into heap space
- 执行 initializer method __ init __() (defined in the body of the class)
- 当__ init __() 执行完毕,返回object(folder)name as final value of expression. (没有return statement)
在执行__ init __(self, … ) 的过程中:
- Passes the folder name to that parameter self
- Passes the other argument in order
- Executes the commands in the body of __ init __()
只要生成一