问答详情
源自:2-5 Python类属性

请问为什么还是运行失败

calss Anmial(object):

    count = 0

    def_init_(self,name,age):

        self.name = name

        self.age = age

        Animal.count + 1

dog = Animal('wangwang',1)

print(Animal.count)

cat = Animal('mimi',3)

print(Animal.count)

pig = Animal('panpan',1)

print(Animal.count)


提问者:20044031 2021-01-23 21:36

个回答

  • 柳随风
    2021-01-23 21:46:52
    已采纳

    calss Anmial(object):

        count = 0

        def __init__(self,name,age):

            self.name = name

            self.age = age

            Animal.count =Animal.count + 1

    dog = Animal('wangwang',1)

    print(Animal.count)

    cat = Animal('mimi',3)

    print(Animal.count)

    pig = Animal('panpan',1)

    print(Animal.count)



  • 慕九州1264712
    2023-03-14 16:31:20

    1、calss错了,2、init两侧是双下划线而不是单下划线,3、冒号有可能是中文模式