关与类中的变量
class Person:
counter = 0
current_year = 2020
is_christmas = False
def __init__(self, full_name, year):
self.full_name = full_name
self.year = year
self.counter +=1
self.id = self.counter
p1 = Person('Homer Simpson', 1982)
print(Person.counter)
为什么Person.counter输出不是1?{:10_262:} 类属性和实例化属性
p1.counter为1 Person.counter为0 改成 Person.counter +=1
页:
[1]