|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
>>> class Person:
population = 0
def __init__(self, name):
self.name = name
print('(Initializing %s)' % self.name)
Person.population += 1
def __del__(self):
print('%s say bye.' % self.name)
Person.population -= 1
print('__del__ 被调用了!!!!!!!!!!')
if Person.population == 0:
print('I am the last one.')
else:
print('There are still %d people left.' % Person.population)
def sayHi(self):
print('Hi,my name is %s' % self.name)
def howMany(self):
if Person.population == 1:
print('I am the only person here.')
else:
print('We have %d persons here.' % Person.population)
>>> swaroop = Person('Swaroop_111111111111')
(Initializing Swaroop_111111111111)
>>> swaroop.sayHi()
Hi,my name is Swaroop_111111111111
>>> swaroop.howMany()
I am the only person here.
>>> kalam = Person('Kalam_222222222222')
(Initializing Kalam_222222222222)
>>> kalam.sayHi()
Hi,my name is Kalam_222222222222
>>> kalam.howMany()
We have 2 persons here.
>>>
>>> swaroop.sayHi()
Hi,my name is Swaroop_111111111111
>>> swaroop.howMany()
We have 2 persons here.
>>> Person.population
2
>>>
|
|