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统计实例的更多相关文章

  1. Python中的staticmethod和classmethod

    谈谈Python中的staticmethod和classmethod 首先值得说明的是staticmethod和classmethod都是python中定义的装饰器,用的时候需要在前面加@ 即@sta ...

  2. (译文)Python中的staticmethod与classmethod

    原文是stackoverflow的一则高票回答,原文链接 可能之前也有人翻译过,但是刚好自己也有疑惑,所以搬运一下,个人水平有限所以可能翻译存在误差,欢迎指正(如侵删). 尽管classmethod和 ...

  3. Python中的@staticmethod和@classmethod的区别

    一直搞不明白,类方法和静态方法的区别,特意研究了一下,跟大家分享一下. 为了方便大家了解两者的差别,以下的示例代码将有助于发现其中的差别: class A(object): def foo(self, ...

  4. 基于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 ...

  5. Python语言特性之3:@staticmethod和@classmethod

    问题:Python中@staticmethod和@classmethod两种装饰器装饰的函数有什么不同? 原地址:http://stackoverflow.com/questions/136097/w ...

  6. python中@staticmethod与@classmethod

    @ 首先这里介绍一下‘@’的作用,‘@’用作函数的修饰符,是python2.4新增的功能,修饰符必须出现在函数定义前一行,不允许和函数定义在同一行.只可以对模块或者类定义的函数进行修饰,不允许修饰一个 ...

  7. python中 staticmethod与classmethod区别

    staticmethod与classmethod区别 参考 https://stackoverflow.com/questions/136097/what-is-the-difference-betw ...

  8. python中 staticmethod与classmethod

    原文地址https://blog.csdn.net/youngbit007/article/details/68957848 原文地址https://blog.csdn.net/weixin_3565 ...

  9. Python - 静态函数(staticmethod), 类函数(classmethod), 成员函数 区别(完全解析)

    原文地址:http://blog.csdn.net/caroline_wendy/article/details/23383995 还有一篇:http://blog.csdn.net/carolzha ...

随机推荐

  1. Asp.Mvc将生成的视图保存为字符串

    public static class ViewExtensions { /// <summary> /// 在控制器内获取指定视图生成后的HTML /// </summary> ...

  2. NSTimer注意内存泄露(真该死)

    NSTimer可以用来执行一些定时任务,比较常用的方法就是: + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTar ...

  3. SlidingMenu+Fragment实现当前最流行的侧滑

    1 http://www.krislq.com/2013/03/android_case_slidingmenu_fragment/ 2 https://github.com/jfeinstein10 ...

  4. Atitit.获取approot api 应用根路径 java c#.net php asp

    Atitit.获取approot api 应用根路径 java c#.net php asp 1. 如果根路径返回empty,否则返回/app,兼容getContextPath() <scrip ...

  5. leetCode 50.Pow(x, n) (x的n次方) 解题思路和方法

    Pow(x, n) Implement pow(x, n). 思路:题目不算难.可是须要考虑的情况比較多. 详细代码例如以下: public class Solution { public doubl ...

  6. spring mvc数据验证

    今天来说一下.前段验证,与后端数据验证.大家都知道.在我们.注册与登陆的时候,往往需要对数据进行效验.那么前段我们都知道,可以使用,js去做处理. 今天主要讲解.后端的数据效验.这里我们采用Hiber ...

  7. Android Studio中常用设置

    参考资料: 1.http://blog.csdn.net/itdada/article/details/43375893 常用设置: 1.Tab不用4个空格Code Style->Java-&g ...

  8. 多媒体开发之--- rtsp 中的H264 编码+打包+解码相关知识es、pes、ts...

    1)ES流(Elementary Stream): 也叫基本码流,包含视频.音频或数据的连续码流. 2)PES流(Packet Elementary Stream): 也叫打包的基本码流, 是将基本的 ...

  9. WebView 显示网页

    1.布局 <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:androi ...

  10. Hadoop学习笔记——Hadoop经常使用命令

    Hadoop下有一些经常使用的命令,通过这些命令能够非常方便操作Hadoop上的文件. 1.查看指定文件夹下的内容 语法: hadoop fs -ls 文件文件夹 2.打开某个已存在的文件 语法: h ...