37,741
社区成员
发帖
与我相关
我的任务
分享class Person():
population=0
def __init__(self,name):
self.name=name
#print 'my name\' is %s' % self.name
Person.population+=1
def sayHi(self):
print 'hello everyone,my name\' is %s'% self.name
def howMany(self):
if Person.population==1:
print "I am the only one here"
else:
print "we have %d people here" % Person.population
def __del__(self):
print '%s say bye,' % self.name
Person.population-=1
if Person.population==0:
print 'I am the last one here'
else:
print 'we still have %d people here' % Person.population
john=Person('john')
john.sayHi()
john.howMany()
smith=Person('smith')
smith.sayHi()
smith.howMany()

*** Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32. ***
>>>
hello everyone,my name' is john
I am the only one here
hello everyone,my name' is smith
we have 2 people here
>>> john say bye,
we still have 1 people here
smith say bye,
I am the last one here