我已经编写了 Person 类,但是每当我使用 Del 时它什么都不做,如果我将其更改为打印,它似乎会删除所有的人,而不仅仅是我问它的那个人。任何解决此问题的帮助将不胜感激。
class Person:
population = 0
def __init__ (self, name, age):
self.name = name
self.age = age
print ("{0} has been born!".format(self.name))
Person.population += 1
def __str__ (self):
return ("{0} is {1} years young.".format(self.name, self.age))
def __del__ (self):
return ("{0} is dying :)".format(self.name))
Person.population -= 1
def Totalpop ():
print ("There are {0} people here mate.".format(Person.population))
p1 = Person("Crumbs", 39)
p2 = Person("Harry", 89846)
p3 = Person("Amanda", 5)
print (p1)
print (p2)
print (p3)
del (p3)
print (Person.Totalpop())