# -*- coding: UTF-8 -*- #属性 查看、修改、删除 #mothod方法 #staticmathod静态的方法 #classmethod 类方法 print("----------------引入") class Goods: disconut = 0.5 def __init__(self,name,price): self.name = name self.__price = price @property def price(self): return self.__price * Goods.disconut apple = Goods('苹果',5) print(apple.price) print("----------------案例") class Goods: __disconut = 0.5 #折扣 def __init__(self,name,price): self.name = name self.__price = price @property def price(self): return self.__price * Goods.__disconut @classmethod #把一个方法变成 类方法,这个方法可以直接被类调用,不需要依托对象 def change__disconut(cls,new_disconut): cls.__disconut = new_disconut apple = Goods('苹果',5) print(apple.price) Goods.change__disconut(0.2) #修改折扣(不需要依托对象) print(apple.price) #当这个方法的操作,只涉及静态属性的时候,就应该使用classmethod来装饰这个方法 class Login: def __init__(self,name,password): self.name = name self.pwd = password @staticmethod def get_usr_pwd(): #静态方法 usr = input("用户名:") pwd = input("密码:") Login(usr,pwd) Login.get_usr_pwd() #在完全面向对象的程序中,如果一个函数,既和对象没有关系,也和类没有关系,那么就用saticmethod将这个函数变成一个静态方法 #类方法和静态方法都是 类调用的 #对象也可以调用 类方法,和静态方法(推荐使用类名调用) #类方法有一个默认参数 cls 代表这个类 #静态方法 没有默认的参数/就像一个函数一样