Python3.x初学者:TypeError:dog()不接受任何参数

Python3.x初学者:TypeError:dog()不接受任何参数,python,Python,错误:回溯(最近一次调用上次):文件 “C:\EclipseWorkspace\csse120\LearnPython\heritation.py”,中的第14行 s=dog('r')类型错误:dog()不接受任何参数 class animal(object): def __init__(self,name): self.name = name def eat(self,food): print("{} is eating".format(sel

错误:回溯(最近一次调用上次):文件 “C:\EclipseWorkspace\csse120\LearnPython\heritation.py”,中的第14行 s=dog('r')类型错误:dog()不接受任何参数

class animal(object):
    def __init__(self,name):
        self.name = name

    def eat(self,food):
        print("{} is eating".format(self.name,food))


class dog():

    def fetch(self,thing):
        print("{} get the {}".format(self.name,thing))

s = dog('r')

你错过了那只狗从动物身上继承下来的东西

Cant figure out whats wrong, please help.

因为您的错误表明您正试图通过将'r'参数传递给Dog类构造函数来创建Dog类的对象。但Dog类并没有接受字符或字符串文本作为参数的构造函数。似乎您正在尝试使用Animal类构造函数来创建Dog对象(Dogs是Animal类的一个子类)

要解决此错误,您应该首先对Dog类进行子类化,然后创建Dog对象。 把动物分类

class animal(object):
  def __init__(self,name):
      self.name = name
现在,您可以使用Animal类构造函数并使用
s=Dog('r')
语句创建Dog对象


提示:-我认为按照样式约定中的描述,从一开始就对类名使用大写字母约定是很好的。

发布答案有点晚,但是尝试在init的每一面添加两个下划线,
不是

应该是

 _init_ 

看起来像是继承练习。:-)也许您忘记继承
类dog
?我认为
s=dog('dog')
应该在类dog块之外。不是吗?
class dog(animal):
  def fetch(self,thing):
      print("{} get the {}".format(self.name,thing))
 _init_ 
__init__
class animal(object):
    def __init__(self,name):
        self.name = name