• day28 classmethod 装饰器


    类方法装饰类方法
      把一个方法变成类中的方法
      之后调用此方法不需要对类实例化后在调用
      直接通过类.方法即可调用

     1 class Goods:
     2     __discount = 0.5
     3     def __init__(self,name,price):
     4         self.name = name
     5         self.__price = price        # 我才不会让别人知道我的价格
     6     @property
     7     def price(self):
     8         return self.__price * Goods.__discount
     9     @classmethod                    # 类方法,把一个方法变成类中的方法,
    10                                     # 这个方法直接可以被类调用,不在需要对象调用了
    11     def chang_dis(cls,new_discount):
    12         Goods.__discount = new_discount
    13 suyang = Goods("苏阳",0.5)            
    14 print(suyang.price)                     # 0.25
    15 Goods.chang_dis(0.3)                   # 原来的方式改还需要输入实例化对象
    16 print(suyang.price)                     # 0.15
  • 相关阅读:
    [ZJOI2010]count 数字计数
    小雄数
    简单筛法函数
    [Noip模拟题]lucky
    欧拉线筛
    Intern Day78
    CodeForces1360C
    CodeForces1373B
    Intern Day78
    Intern Day78
  • 原文地址:https://www.cnblogs.com/shijieli/p/9922895.html
Copyright © 2020-2023  润新知