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 ...
随机推荐
- STL学习笔记(迭代器配接器)
Reverse(逆向)迭代器 Reverse迭代器是一种配接器. 重新定义递增运算和递减运算.使其行为正好倒置. 如果你使用这类迭代器,算法将以逆向次序处理元素.所有标准容器都允许使用Reverse迭 ...
- Android Shader 颜色、图像渲染 paint.setXfermode
Shader Shader是一个基类,表示在绘制期间颜色的水平跨度 它的子类被嵌入在Paint中使用,调用paint.setShader(shader). 除Bitmap外的其他对象,使用该Paint ...
- 【Django】01_创建一个简单的项目
1.创建Django项目 点击:file-->new project,出现下面的对话框.选择Django栏目,输入项目名称, 选择python解释器版本,点击create创建. Django将自 ...
- 超高逼格Log日志打印
代码地址如下:http://www.demodashi.com/demo/12646.html 前言 Log日志的打印一直是一个比较头疼的事,怎样才能让自己的log显示更多信息,怎样才能让自己的log ...
- 高抛低吸T+0操作要领(目前行情短线炒作的必备技能)
最近的行情只能用操蛋来形容,但是危机中不乏机会.现在已经不是之前行情的思路,那着一个股票长线抱着,即使是好的牛股,也经不起目前行情的这 么折腾.所以,现在最适合的操作方式就是高抛低吸.今天低吸保不准明 ...
- php单元测试入门教程phpunit详解
本文档提供了一些phpunit官方教程没有提到的信息,帮助初学者快速了解php单元测试,在phpunit官网提供了详细的中文教程,可选多种格式下载 phpunit官网地址:https://phpuni ...
- hdu 4601 Letter Tree
不easy啊.. 一个小错误让我wa死了.找了一个晚上,怎么都找不到 最后是对拍代码找到的错误.发现当步数比較小的时候答案就是对的,比較大的时候就不正确了 想到一定是什么地方越界了.. . power ...
- emacs的常用配置备份
据说有人搞丢了自己的emacs的配置,然后一怒之下抛弃了emacs投身vim,我还是做个emacs配置的备份吧, 虽然我现在也算不上emacs的发烧友. 这里的配置大多是从网上参考的,最多的是下面的链 ...
- mac上搭建docker镜像私服
1.创建私服容器 docker run -d -e SETTINGS_FLAVOR=dev -e STORAGE_PATH=/tmp/registry -v /opt/data/registry:/t ...
- JQ多种刷新方式
下面介绍全页面刷新方法:有时候可能会用到 window.location.reload()刷新当前页面. parent.location.reload()刷新父亲对象(用于框架) opener.loc ...