【发布时间】:2018-03-09 07:42:23
【问题描述】:
我使用的是 python 2.7,我创建了一个 Dog 父类和一个 Cat 子类;我已经将 Cat 类“分解”为两部分,这两个部分本来是要链接在一起的,但是却抛出了一个错误:
关于如何解决这个问题的任何想法,以及这里传递参数的逻辑?:
class Dog(object):
def __init__(self, name):
self.name = name
self.age = 6
def age_update(self, age_update):
self.age+=age_update
print("new age: " + str(self.age))
def speak (self):
print("My name is "+ self.name + "and I'm "+ str(self.age))
class Cat(Dog):
def __init__(self):
super(Cat, self).__init__(name)
self.lives=CatNature()
class CatNature(Cat):
def __init__(self, lives=9):
self.lives=lives
def show_lives(self):
print("This cat has " + str(self.lives) + " lives")
cat1 = Cat("Fuzz")
print ("Cat's name is " + cat1.name + " and " + str(cat1.age))
cat1.lives.show_lives()
错误:
Traceback (most recent call last):
File "/Users/.../Python_experiments/class.py", line 27, in <module>
cat1 = Cat("Fuzz")
TypeError: __init__() takes exactly 1 argument (2 given)
【问题讨论】:
-
猫就是狗?
-
;-) 是的,我在命名类时很仓促。为了这个实验,我的想法是猫和狗都可以共享有名字和年龄的品质......并且被错误分心而无法回到那个问题 - 我会研究你的解决方案 - 并做出回应有任何其他问题 - 谢谢!
标签: python python-2.7 class arguments