File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 5252- [ 内置函数] ( ./built-in-functions.md )
5353- [ 类] ( ./class.md )
5454 - [ 类属性、实例属性] ( ./class-instance-props.md ) …
55+ - [ 类的属性与方法] ( ./functions-props.md )
5556 - [ __ slots__ ] ( ./slots.md ) …
5657 - [ @property ] ( ./@property.md ) …
5758 - [ 定制类] ( ./custom-class.md ) …
Original file line number Diff line number Diff line change 1+ # 类的属性与方法
2+
3+ - ` 类 ` 属性
4+ ``` python
5+ class T :
6+ props = None # 这个就是类属性
7+ ```
8+ - `实例` 属性
9+ ```python
10+ t = T()
11+ t.props = 1 # 实例属性
12+ ```
13+
14+ 注: 通过实例访问属性时,如果 ** 实例对象没有找到** ,会 “向上查找” `类属性`
15+
16+ ----
17+
18+ - `类方法`
19+ ```python
20+ class T :
21+ @ classmethod
22+ def method (cls ): # cls: Python 自动添加当前类引用
23+ pass
24+
25+ T.method()
26+ ```
27+ - `实例方法`
28+ ```python
29+ class T :
30+ def method (self ):
31+ pass
32+
33+ t = T()
34+ t.method()
35+ ```
36+ - `静态方法`
37+ ```python
38+ class T :
39+ @ staticmethod
40+ def method ():
41+ pass
42+
43+ T.method()
44+ ```
You can’t perform that action at this time.
0 commit comments