017: class, objects and instance: class method
2016-01-24 21:38
253 查看
类的方法
所谓类的方法,也就是,这个方法会绑定到一个类上面,实例化一个instance的时候,这个方法不会再重新生成 一份,它只有访问类级别的变量
它用@classmethod标签来标注这是一个class method.
执行结果为:
所谓类的方法,也就是,这个方法会绑定到一个类上面,实例化一个instance的时候,这个方法不会再重新生成 一份,它只有访问类级别的变量
它用@classmethod标签来标注这是一个class method.
class Book(object): num = 10 # instance method, will be bound to an object def __init__(self, title, price): self.title = title self.price = price # class method, will not be bound to an object @classmethod def display(cls): print("\n*******************************") print("this is a class method", cls.num) print("===============================\n") book = Book("Python Basic", 25) Book.display() book.display() print("\n*******************************") print("<class method essence>") print(Book.display) print(book.display) print("===============================\n")
执行结果为:
******************************* this is a class method 10 =============================== ******************************* this is a class method 10 =============================== ******************************* <class method essence> <bound method type.display of <class '__main__.Book'>> <bound method type.display of <class '__main__.Book'>> ===============================
相关文章推荐
- Rich feature hierarchies for accurate object detection and semantic segmentation论文笔记
- Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks论文笔记
- 使用SoapObject登入网站获取通行证
- [8]姥爷幽默谈Objective-C-继承,多态,封装
- JSONArray 的 fromObject()方法执行时报错问题
- 016: class, objects and instance: instance method
- Object-C--->Swift之(七)嵌套函数与闭包
- Condition.await, signal 与 Object.wait, notify 的区别
- java泛型中的? 、Object、? extends Object的区别
- 016: class and objects > 多重继承与多态的例子
- Objective-c开发教程--MRC和ARC混编
- Cannot locate the chosen ObjectFactory implementation: spring
- Objective-C UI之自定义轮播图控件
- 在swift 项目中使用objective-c
- 关于EnumerateObjectsUsingBlock和for-in之间的较量
- Object-C--->Swift之(七)函数重载
- Iterable Object, Iterator, Generator, Generator Iterator
- Object-C--->Swift之(六)函数类型
- 编码运行两不误!KZPlayground,Objective-C中的Playgrounds
- JS 解析map 格式JSON super.makeJSONObject(req, res, map);