linux:python:使用class变量
差别
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录前一修订版后一修订版 | 前一修订版 | ||
linux:python:使用class变量 [2020/09/12 18:29] – zhangguo | linux:python:使用class变量 [2023/03/17 10:12] (当前版本) – 外部编辑 127.0.0.1 | ||
---|---|---|---|
行 10: | 行 10: | ||
- class b在class a实例化时进行赋值,之后class a一直以classmethod的方式使用。 | - class b在class a实例化时进行赋值,之后class a一直以classmethod的方式使用。 | ||
- | - class b的实例化在class a的一个classmethod方法中完成,class a的方法总是以classmethod的方式进行使用。以在使用的角度上来看,或许此种方式看起来舒服一些。 | + | - class b的实例化在class a的一个classmethod方法中完成,class a的方法总是以classmethod的方式进行使用。<color #ed1c24>在使用的角度上来看,或许此种方式看起来舒服一些。</ |
<code python> | <code python> | ||
行 16: | 行 16: | ||
def __init__(self, | def __init__(self, | ||
self.name = name | self.name = name | ||
- | def print(self): | + | def myprint(self): |
print(f' | print(f' | ||
行 30: | 行 30: | ||
| | ||
@classmethod | @classmethod | ||
- | def print(cls): | + | def myprint(cls): |
- | a.b_inst.print() | + | a.b_inst.myprint() |
| | ||
| | ||
if __name__ == ' | if __name__ == ' | ||
a_inst = a(' | a_inst = a(' | ||
- | a.print() # 调用class a的classmethod, 方法里有使用到class a的类变量。 | + | a.myprint() # 调用class a的classmethod, 方法里有使用到class a的类变量。 |
| | ||
# or | # or | ||
a.set_name(' | a.set_name(' | ||
- | a.print() # 调用class a的classmethod, 方法里有使用到class a的类变量。 | + | a.myprint() # 调用class a的classmethod, 方法里有使用到class a的类变量。 |
</ | </ | ||
linux/python/使用class变量.1599906596.txt.gz · 最后更改: 2023/03/17 10:12 (外部编辑)