是不是類方法訪問類屬性要用cls,而實例方法只能用self
class?Person(object): ????__count?=?0 ????@classmethod ????def?how_many(self): ????????return?self.__count ????def?__init__(cls,?name): ????????cls.name?=?name ????????Person.__count?+=?1 print(Person.how_many()) p1?=?Person('Bob') print(Person.how_many())
是不是類方法訪問類屬性要用cls,而實例方法只能用self,我將它們反過來也可以運行。
是不是規定好了,【類方法】將訪問類屬性傳入參數用cls,【實例方法】傳入的參數用self