码迷,mamicode.com
首页 > 编程语言 > 详细

Python Special Syntax 5:对象相关

时间:2014-07-05 20:28:10      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   使用   python   div   

__init__方法一样,还有一个特殊的方法__del__,它在对象消逝的时候被调用。但是很难保证这个方法究竟在 什么时候 运行。如果你想要指明它的运行,你就得使用del语句,

#-*-coding:utf-8
class Person:
    __privateName=None;#私有变量

    count=0;#既可以被实例对象引用,也可以被当做静态对象引用,NND,好混乱。

    def __init__(self,name):
        self.count+=1
        Person.count+=2
        print(‘%s has been created‘ % name)
        self.__privateName=name;
        self.name=name

    def printPrivateName(self):
        print(‘Private Name: %s‘ %  self.__privateName)

    def printName(self):
        print(‘Name is %s‘ % self.name)



zili=Person(‘zili‘)
Person.__privateName=‘zhangsan‘
print(Person.__privateName)
zili.__privateName=‘zhangsan‘
print(‘jiangyao shuchu :%s‘ % zili.__privateName)
zili.printName()
zili.printPrivateName()
print(zili.count)
print(Person.count)
Person.ABC=‘abcvalue‘
print(Person.ABC)

Output:

zili has been created
zhangsan
jiangyao shuchu :zhangsan
Name is zili
Private Name: zili
1
2
abcvalue

 

Python Special Syntax 5:对象相关,布布扣,bubuko.com

Python Special Syntax 5:对象相关

标签:style   blog   color   使用   python   div   

原文地址:http://www.cnblogs.com/yanyuge/p/3822409.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!