静态方法:staticmethod,不需要实例化的方法,
class Login(object):
@staticmethod
def login(username, password):
print(username, password)
Login.login('test','123456') # test 123456
类方法:classmethod 类中方法只涉及修改静态属性
class Goods(object):
__discount = 0.9
def __init__(self, name, price):
super(Goods, self).__init__()
self.name = name
self.__price = price
@property
def price(self):
return self.__price * self.__discount
@classmethod
def change_discount(cls, new_discount):
cls.__discount = new_discount
apple = Goods('apple', 6)
print(apple.price) # 5.4
Goods.change_discount(1)
print(apple.price) # 6