问答详情
源自:2-3 Python实例属性的定义

请问这个哪里错了?

class Animal(object):

    def_init_(self,name,age):

        self.name = name

        self.age = age

dog = Animal('Tim',4) 

cat = Animal('Aim',5)

print(dog.name)

print(dog.age)

print(cat.name)

print(cat.age)


提问者:20044031 2021-03-08 21:12

个回答

  • qq_关怀天下_0
    2022-02-11 15:39:20

    class Animal():
        def __init__(self,name,age):
            self.name = name
            self.age = age
            print('{}今年{}岁'.format(name,age))
    
    dog = Animal('汪汪',18)
    cat = Animal('喵喵',30)


  • 慕尼黑3176241
    2021-03-09 00:21:18

    class Animal(object):
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
    
    dog = Animal('Tim', 4)
    cat = Animal('Aim', 5)
    print(dog.name)
    print(dog.age)
    print(cat.name)
    print(cat.age)

    def后有个空格,init前后是两个下划线