zoukankan      html  css  js  c++  java
  • 修改类属性 分类: divide into python 2013-11-06 16:48 236人阅读 评论(0) 收藏

    例子一:

    一下是修改类属性,

    class counter:
        count = 0  
        def __init__(self):
            self.__class__.count+=1 #此时类属性被类和所有类实例共享
    
    print counter.count # 0
    c=counter()
    print c.count # 1
    
    d=counter() 
    print d.count #2
    
    print counter.count #2

    例子二:


    class counter(object):
        count=0
        class_co=0
        def __init__(self):
            self.count+=1  #实例所拥有
            counter.class_co+=1
    
    print counter.count,counter.class_co # 0 0
    
    c=counter()
    print c.count,c.class_co # 1 1
    
    a=counter()
    print a.count,c.class_co # 1 2


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    edu_6_1_4
    edu_6_1_2
    edu_6_1_3
    edu_6_1_1
    音乐链接
    音乐推荐界面
    客服页面
    购物页面
    京东读书新闻资讯页面
    安装Tomcat时 ,设置JAVA_HOME和JRE_HOME
  • 原文地址:https://www.cnblogs.com/think1988/p/4628027.html
Copyright © 2011-2022 走看看