為什么這樣結果是0
class A():
? ? __count=0
? ? def __init__(self):
? ? ? ? __count=1
? ??
? ? @classmethod
? ? def get_count(cls):
? ? ? ? return cls.__count
? ? pass
B=A()
print(A.get_count())
class A():
? ? __count=0
? ? def __init__(self):
? ? ? ? __count=1
? ??
? ? @classmethod
? ? def get_count(cls):
? ? ? ? return cls.__count
? ? pass
B=A()
print(A.get_count())
2024-12-28
舉報
2025-01-29
第4行的__count是__init__方法里一個局部變量, 跟第2行的__count沒有什么關系,并不會改變第2行的類私有屬性的值。
2024-12-31
你這里第4行寫的是定義__count = 1,而不是使__count加上一,應該為
__count += 1