【发布时间】:2019-08-07 10:45:30
【问题描述】:
我知道这是一个菜鸟问题,但我正在学习 OOP,无法计算得到的输出
这是我找到的代码,我可以知道它是如何运行的吗?
class InstanceCounter(object):
count = 0
def __init__(self, val):
self.val = val
InstanceCounter.count += 1
def set_val(self, newval):
self.val = newval
def get_val(self):
print(self.val)
def get_count(self):
print(InstanceCounter.count)
a = InstanceCounter(5)
b = InstanceCounter(10)
c = InstanceCounter(15)
for obj in (a, b, c):
print("value of obj: %s" % obj.get_val())
print("Count : %s" % obj.get_count())
【问题讨论】:
-
我猜你得到的输出是 5, 3, 10, 3, 15, 3 ?
-
是的,它是正确的,但我可以知道我们是如何得到的吗
-
我可以知道为什么我们在 5 之后得到 3 吗?
标签: python-3.x