Python定义类方法
2015-06-25 22:00
736 查看
和属性类似,方法也分实例方法和类方法。
在class中定义的全部是实例方法,实例方法第一个参数 self 是实例本身。
要在class中定义类方法,需要这么写:
通过标记一个 @classmethod,该方法将绑定到 Person 类上,而非类的实例。类方法的第一个参数将传入类本身,通常将参数名命名为 cls,上面的 cls.count 实际上相当于 Person.count。
因为是在类上调用,而非实例上调用,因此类方法无法获得任何实例变量,只能获得类的引用。
如果将类属性 count 改为私有属性__count,则外部无法读取__score,但可以通过一个类方法获取,请编写类方法获得__count值。
?不会了怎么办
注意类方法需要添加 @classmethod
参考代码:
在class中定义的全部是实例方法,实例方法第一个参数 self 是实例本身。
要在class中定义类方法,需要这么写:
class Person(object): count = 0 @classmethod def how_many(cls): return cls.count def __init__(self, name): self.name = name Person.count = Person.count + 1 print Person.how_many() p1 = Person('Bob') print Person.how_many()
通过标记一个 @classmethod,该方法将绑定到 Person 类上,而非类的实例。类方法的第一个参数将传入类本身,通常将参数名命名为 cls,上面的 cls.count 实际上相当于 Person.count。
因为是在类上调用,而非实例上调用,因此类方法无法获得任何实例变量,只能获得类的引用。
任务
如果将类属性 count 改为私有属性__count,则外部无法读取__score,但可以通过一个类方法获取,请编写类方法获得__count值。?不会了怎么办
注意类方法需要添加 @classmethod
参考代码:
class Person(object): __count = 0 @classmethod def how_many(cls): return cls.__count def __init__(self, name): self.name = name Person.__count = Person.__count + 1 print Person.how_many() p1 = Person('Bob') print Person.how_many()
相关文章推荐
- Python访问限制
- Python初始化实例属性
- python 解析HTML
- python模拟登陆知乎和CSDN【urllib,re,requests】
- 用Python和Pygame写游戏-从入门到精通(21)
- 用Python和Pygame写游戏-从入门到精通(22)
- 用Python和Pygame写游戏-从入门到精通(20)
- 用Python和Pygame写游戏-从入门到精通(19)
- 用Python和Pygame写游戏-从入门到精通(17)
- 用Python和Pygame写游戏-从入门到精通(18)
- 用Python和Pygame写游戏-从入门到精通(15)
- 用Python和Pygame写游戏-从入门到精通(14)
- Python使用__future__
- 用Python和Pygame写游戏-从入门到精通(13)
- 用Python和Pygame写游戏-从入门到精通(11)
- Python动态导入模块
- 用Python和Pygame写游戏-从入门到精通(10)
- 用Python和Pygame写游戏-从入门到精通(9)
- 【python】 入门 - 函数式编程
- 用Python和Pygame写游戏-从入门到精通(7)