Python OOP(3) staticmethod和classmethod统计实例
staticmethod 统计实例
#!python2
#-*- coding:utf-8 -*- class c1: amount_instance=0 def __init__(self):
c1.amount_instance+=1 @staticmethod def printresult():
print "Amount of Instance: " ,c1.amount_instance class sub1(c1): #此处定义没有作用,因为只会引用c1中amount_instance amount_instance=0 @staticmethod def printresult(): print "call by sub1" c1.printresult() class sub2(c1): #此处定义没有作用,因为只会引用c1中amount_instance amount_instance=0 i1,i2=c1(),c1() i1.amount_instance
Amount of Instance: 2 c1.amount_instance Amount of Instance: 2 s1,s2=sub1(),sub1() s1.printresult() call by sub1
Amount of Instance: 4 sub1.printresult() call by sub1
Amount of Instance: 4 s3=sub2() s3.printresult() Amount of Instance: 5
classmethod 统计实例
#!python2
#-*- coding:utf-8 -*-
#利用类方法管理每个类的实例计数器
#父类使用一个类方法来管理状态信息,该信息根据树中的类不同而不同,而且储存在类上
#类方法调用的是当前类的amount_instance而不是父类中的amount_instance class c1: amount_instance=0 @classmethod def printresult(cls): cls.amount_instance+=1 def __init__(self): self.printresult() class sub1(c1): amount_instance=0 def __init__(self): c1.__init__(self) class sub2(c1): amount_instance=0 i1=c1() i1.amount_instance
1 c1.amount_instance
1 s1,s2=sub1(),sub1() s1.amount_instance
2
sub1.amount_instance
2
c1.amount_instance
1 s3,s4,s5=sub2(),sub2(),sub2() s3.amount_instance
3 c1.amount_instance
1
所以
1)静态方法:适用于处理一个类的本地数据
2)类方法:适用于处理类层级中每个类的数据
Python OOP(3) staticmethod和classmethod统计实例的更多相关文章
- Python中的staticmethod和classmethod
谈谈Python中的staticmethod和classmethod 首先值得说明的是staticmethod和classmethod都是python中定义的装饰器,用的时候需要在前面加@ 即@sta ...
- (译文)Python中的staticmethod与classmethod
原文是stackoverflow的一则高票回答,原文链接 可能之前也有人翻译过,但是刚好自己也有疑惑,所以搬运一下,个人水平有限所以可能翻译存在误差,欢迎指正(如侵删). 尽管classmethod和 ...
- Python中的@staticmethod和@classmethod的区别
一直搞不明白,类方法和静态方法的区别,特意研究了一下,跟大家分享一下. 为了方便大家了解两者的差别,以下的示例代码将有助于发现其中的差别: class A(object): def foo(self, ...
- 基于python中staticmethod和classmethod的区别(详解)
例子 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 class A(object): def foo(self,x): print "executing foo ...
- Python语言特性之3:@staticmethod和@classmethod
问题:Python中@staticmethod和@classmethod两种装饰器装饰的函数有什么不同? 原地址:http://stackoverflow.com/questions/136097/w ...
- python中@staticmethod与@classmethod
@ 首先这里介绍一下‘@’的作用,‘@’用作函数的修饰符,是python2.4新增的功能,修饰符必须出现在函数定义前一行,不允许和函数定义在同一行.只可以对模块或者类定义的函数进行修饰,不允许修饰一个 ...
- python中 staticmethod与classmethod区别
staticmethod与classmethod区别 参考 https://stackoverflow.com/questions/136097/what-is-the-difference-betw ...
- python中 staticmethod与classmethod
原文地址https://blog.csdn.net/youngbit007/article/details/68957848 原文地址https://blog.csdn.net/weixin_3565 ...
- Python - 静态函数(staticmethod), 类函数(classmethod), 成员函数 区别(完全解析)
原文地址:http://blog.csdn.net/caroline_wendy/article/details/23383995 还有一篇:http://blog.csdn.net/carolzha ...
随机推荐
- 【Excle数据透视表】如何让字段标题不显示“求和项”
我们做好了数据透视表之后是下面这个样子的 这个样子一点都不好看,那么如何去掉"求和项"呢? 步骤 方法① 单击B3单元格→编辑区域输入"数量 "→Enter(也 ...
- PLSQL Developer设置及快捷键设置
1.登录后默认自动选中My Objects 默认情况下,PLSQL Developer登录后,Brower里会选择All objects,如果你登录的用户是dba,要展开tables目录,正常情况都需 ...
- Android 开发 Eclipse使用SVN
1 help--->install new software--->add 2 name自定义 location填入内容见3 3 http://subclipse.tigris.org/s ...
- Python--多进程--01
multiprocess import multiprocessing import time def worker_1(interval): print(' i am worker1') n=5 w ...
- The TTY demystified
http://www.linusakesson.net/programming/tty/index.php The TTY demystified Real teletypes in the 1940 ...
- 浅谈"壳"(一)
壳,即坚硬的外皮,当壳的厚度与其曲面率半径的比值小于0.5时.称为"薄壳".反之称为"厚壳".由壳演化来的胸甲,盾牌. 在计算机这个注重创意又不失从文化科技中汲 ...
- Linux上部署Java应用+Python3环境搭建
给了Linux的测试环境,目前需要install JDK, Tomcat,此处记录下小白的操作过程. 1. 查询Linux发行版本,包括内核信息 (1) Linux查询内核信息 $ uname -a ...
- 如何在struts2中实现下载?
<a href="${pageContext.request.contextPath}/download?filename="+filename>点击下载</a& ...
- GDB + gdbserver 远程调试mediaserver进程
远程调试步骤 在Android设备上启动gdbserver并attach你想调试的进程,并指定监听调试命令的端口(此端口是TV上的端口) $ adb shell # ps |grep media # ...
- json性能测试
http://www.open-open.com/lib/view/open1434377191317.html