【3】

‘’’
3、在一个类中定义一个变量,
用于跟踪该类有多少个实例被创建(当实例化一个对象,这个变量+1,
当销毁一个对象,这个变量自动-1)
‘’’

class Person:
    count=0
    def __init__(self):
        Person.count+=1
    def __del__(self):
        Person.count-=1
p=Person()
print(Person.count)
del p
print(Person.count)

导航栏

猜你喜欢

转载自blog.csdn.net/qq_43586192/article/details/111834252