# python 类相关
# /usr/sbin/py/python
# -*-coding:utf8-*-
# python 中的静态属性,静态方法 类方法
# 静态属性 实际就是数据属性
class person:
def __init__(self,name,age):
self.name = name
self.age = age
@property
def run(self):
print("%s is running" %self.name)
@classmethod
def cmethod(cls, *args):
print(args)
@staticmethod
def singing(*args):
print(args)
p1 =person("jake",10)
p1.run #添加了 property注解以后 可以通过调用属性的方式去调用方法,
# 通过这种方式可以 隐藏一些逻辑 调用者不会有任何感知
# 类方法
person.cmethod(1,2,3,4) #使用 classmethod注解 定义类方法,可通过类直接调用
# 静态方法
person.singing("song") # 使用 staticmethod 注解 和类完全不相关
python 20 类中的方法
最新推荐文章于 2024-12-16 21:08:24 发布
11万+

被折叠的 条评论
为什么被折叠?



