01. Class Structure
The term 1.1 - Example
- The use of object-oriented development, the first step is to design class
- Use the class name () to create objects, create objects of action has two steps:
- 1) for the object in memory space allocated
- 2) calls the initialization method
__init__
for the object initialization
- After the object is created, the memory of the object will have a real presence - Examples
So, it will usually put:
- Created out of an object called a class of instances
- Creating an object motion is called instantiation
- Properties of an object is called an instance attribute
- Method called object is called an instance method
During program execution:
- Objects have their own instance properties
- Call the object methods, you can
self.
- Access to your property
- Call your own way
in conclusion
- Each object has its own separate memory space , saving their different properties
- The method of multiple objects , only one in memory , when you call the method, the need to reference the object inside passed to the method
1.2 is a special object class
Python
In all objects are :
class AAA:
Defined category belongs to the class objectobj1 = AAA()
Belonging to the instance of an object
- The program is running, type the same will be loaded into memory
- In
Python
, the class is a special object - class object - The program runs, the class object in memory, only one , the use of a class can create a number of object instances
- In addition to the package Examples of attributes and methods , the class object can also have its own attributes and methods
- Class Properties
- Class Methods
- By class name. The way you can attribute access class or method calls the class
02. class and instance properties
2.1 The concept and use
- Class attribute is a class object defined attributes
- It is generally used to record relevant to this class feature
- Class attribute does for recording characteristics of the particular object
Examples demand
- Define a Tools
- Each tool has its own
name
- Demand - know to use this class, create a number of tools objects?
Tool class (Object): # Use an assignment statement, the definition of class properties, record the total number of objects creation tool COUNT = 0 DEF __init __ (Self, name): self.name = name # do a +1 count for the class attribute Tool.count + = 1 # Builder objects tool1 = tool ( "ax") TOOL2 = tool ( "hammer") tool3 = tool ( "shovel") # known to use tool class in the end created a number of objects? Print ( "now created% d a tool "% Tool.count)
2.2 property acquisition mechanism (science)
- In
Python
the acquisition of the property there is a look up mechanism
- 因此,要访问类属性有两种方式:
- 类名.类属性
- 对象.类属性 (不推荐)
注意
- 如果使用
对象.类属性 = 值
赋值语句,只会 给对象添加一个属性,而不会影响到 类属性的值
03. 类方法和静态方法
3.1 类方法
- 类属性 就是针对 类对象 定义的属性
- 使用 赋值语句 在
class
关键字下方可以定义 类属性 - 类属性 用于记录 与这个类相关 的特征
- 使用 赋值语句 在
- 类方法 就是针对 类对象 定义的方法
- 在 类方法 内部可以直接访问 类属性 或者调用其他的 类方法
语法如下
@classmethod def 类方法名(cls): pass
- 类方法需要用 修饰器
@classmethod
来标识,告诉解释器这是一个类方法 - 类方法的 第一个参数 应该是
cls
- 由 哪一个类 调用的方法,方法内的
cls
就是 哪一个类的引用 - 这个参数和 实例方法 的第一个参数是
self
类似 - 提示 使用其他名称也可以,不过习惯使用
cls
- 由 哪一个类 调用的方法,方法内的
- 通过 类名. 调用 类方法,调用方法时,不需要传递
cls
参数 - 在方法内部
- 可以通过
cls.
访问类的属性 - 也可以通过
cls.
调用其他的类方法
- 可以通过
示例需求
- 定义一个 工具类
- 每件工具都有自己的
name
- 需求 —— 在 类 封装一个
show_tool_count
的类方法,输出使用当前这个类,创建的对象个数
@classmethod def show_tool_count(cls): """显示工具对象的总数""" print("工具对象的总数 %d" % cls.count)
在类方法内部,可以直接使用
cls
访问 类属性 或者 调用类方法
3.2 静态方法
-
在开发时,如果需要在 类 中封装一个方法,这个方法:
- 既 不需要 访问 实例属性 或者调用 实例方法
- 也 不需要 访问 类属性 或者调用 类方法
-
这个时候,可以把这个方法封装成一个 静态方法
语法如下
@staticmethod def 静态方法名(): pass 静态方法 需要用 修饰器 @staticmethod 来标识,告诉解释器这是一个静态方法 通过 类名. 调用 静态方法 class Dog(object): # 狗对象计数 dog_count = 0 @staticmethod def run(): # 不需要访问实例属性也不需要访问类属性的方法 print("狗在跑...") def __init__(self, name): self.name = name
3.3 方法综合案例
需求
- 设计一个
Game
类 - 属性:
- 定义一个 类属性
top_score
记录游戏的 历史最高分 - 定义一个 实例属性
player_name
记录 当前游戏的玩家姓名
- 定义一个 类属性
- 方法:
- 静态方法
show_help
显示游戏帮助信息 - 类方法
show_top_score
显示历史最高分 - 实例方法
start_game
开始当前玩家的游戏
- 静态方法
- 主程序步骤
- 1) 查看帮助信息
- 2) 查看历史最高分
- 3) 创建游戏对象,开始游戏
案例小结
- 实例方法 —— 方法内部需要访问 实例属性
- 实例方法 内部可以使用 类名. 访问类属性
- 类方法 —— 方法内部 只 需要访问 类属性
- 静态方法 —— 方法内部,不需要访问 实例属性 和 类属性
提问
如果方法内部 即需要访问 实例属性,又需要访问 类属性,应该定义成什么方法?
答案
- 应该定义 实例方法
- 因为,类只有一个,在 实例方法 内部可以使用 类名. 访问类属性
class Game(object): # 游戏最高分,类属性 top_score = 0 @staticmethod def show_help(): print("帮助信息:让僵尸走进房间") @classmethod def show_top_score(cls): print("游戏最高分是 %d" % cls.top_score) def __init__(self, player_name): self.player_name = player_name def start_game(self): print("[%s] 开始游戏..." % self.player_name) # 使用类名.修改历史最高分 Game.top_score = 999 # 1. 查看游戏帮助 Game.show_help() # 2. 查看游戏最高分 Game.show_top_score() # 3. 创建游戏对象,开始游戏 game = Game("小明") game.start_game() # 4. 游戏结束,查看游戏最高分 Game.show_top_score()