python的static方法和class方法

class Caculator(object):
    name = "caculator"
def __init__(self, x, y):
self._x = x
self._y = y @property
def add(self):
return self._x + self._y @classmethod
def class_info(cls): #能够被类和对象调用;入参为cls,只能够访问类的变量,不能够访问对象的变量.
print("this is a" , cls.name) @staticmethod #能够被类和对象调用;但入参不能为self和cls,因此不能访问对象和类的属性
def static_show(a, b):
print(a, b) if __name__ == "__main__":

cal = Caculator(1, 2)

Caculator.class_info()

cal.class_info()

Caculator.static_show(3, 4)

cal.static_show(3, 4)

上一篇:linux系统下安装单台Redis


下一篇:Flask入门的第一个项目