# 練習一個類一共建立了幾個對象
class Person():
count = 0
def __new__(cls, *args, **kwargs):
cls.count += 1
return object.__new__(cls)
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person('zhangsan', 21)
p2 = Person('lisi', 18)
p3 = Person('wangwu', 23)
print(Person.count)
# 練習一個類一共建立了幾個對象
class Person():
__count = 0
def __new__(cls, *args, **kwargs):
cls.__count += 1
return object.__new__(cls)
def __init__(self, name, age):
self.name = name
self.age = age
@classmethod
def get_count(cls):
return cls.__count
p1 = Person('zhangsan', 21)
p2 = Person('lisi', 18)
p3 = Person('wangwu', 23)
print(Person.get_count())
# 練習一個類一共建立了幾個對象
class Person():
count = 0
def __init__(self, name, age):
Person.count += 1
self.name = name
self.age = age
p1 = Person('zhangsan', 21)
p2 = Person('lisi', 18)
p3 = Person('wangwu', 23)
print(Person.count)