鱼C论坛

 找回密码
 立即注册
查看: 1159|回复: 5

[已解决]我父类的继承出问题啦

[复制链接]
发表于 2018-6-26 20:24:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
class Bird(object):
    def __init__(self, name):
        self.name=name
        
    def greet(self):
        print("Hello, I am %s" % self.name)

        
class HungryBird(Bird):
    def __init__(self):
        super(HungryBird, self).__init__()
        self.hungry=True
        
    def eat(self):
        if self.hungry:
            print ("Aaaah")
            self.hungry=False
        else:
            print ("I am full")

代码如上,在实例化HungryBird的时候,总反馈我输入的参数数量不对,晕死!

>>> hb=HungryBird("wuya")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:/Users/Administrator/Desktop/temp2.py", line 13, in __init__
    super(HungryBird, self).__init__()
TypeError: __init__() takes exactly 2 arguments (1 given)
>>>

>>> hb=HungryBird()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:/Users/Administrator/Desktop/temp2.py", line 13, in __init__
    super(HungryBird, self).__init__()
TypeError: __init__() takes exactly 2 arguments (1 given)

快晕死了,求救啊!
最佳答案
2018-6-26 20:54:55
本帖最后由 凌九霄 于 2018-6-26 20:56 编辑
  1. class Bird(object):
  2.     def __init__(self, name):
  3.         self.name = name

  4.     def greet(self):
  5.         print("Hello, I am %s" % self.name)


  6. class HungryBird(Bird):
  7.     def __init__(self,name):   #<---
  8.         super(HungryBird, self).__init__(name)
  9.         self.hungry = True

  10.     def eat(self):
  11.         if self.hungry:
  12.             print("Aaaah")
  13.             self.hungry = False
  14.         else:
  15.             print("I am full")
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-6-26 20:40:33 | 显示全部楼层
super(HungryBird,self).__init__(name)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-26 20:43:40 From FishC Mobile | 显示全部楼层
父类初始化需要传入参数,你的子类初始化函数没有传参,肯定报错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-6-26 20:46:44 | 显示全部楼层
代码改成:
class Bird(object):
    def __init__(self, name):
        self.name=name
        
    def greet(self):
        print("Hello, I am %s" % self.name)

        
class HungryBird(Bird):
    def __init__(self):
        super(HungryBird, self).__init__(name)
        self.hungry=True
        
    def eat(self):
        if self.hungry:
            print ("Aaaah")
            self.hungry=False
        else:
            print ("I am full")

然后实例化

>>> hb=HungryBird("wuya")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() takes exactly 1 argument (2 given)

还是报错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-26 20:54:55 | 显示全部楼层    本楼为最佳答案   
本帖最后由 凌九霄 于 2018-6-26 20:56 编辑
  1. class Bird(object):
  2.     def __init__(self, name):
  3.         self.name = name

  4.     def greet(self):
  5.         print("Hello, I am %s" % self.name)


  6. class HungryBird(Bird):
  7.     def __init__(self,name):   #<---
  8.         super(HungryBird, self).__init__(name)
  9.         self.hungry = True

  10.     def eat(self):
  11.         if self.hungry:
  12.             print("Aaaah")
  13.             self.hungry = False
  14.         else:
  15.             print("I am full")
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-6-26 20:57:26 | 显示全部楼层
搞定了,
class HungryBird(Bird):也要改成:
class HungryBird(Bird,name):

谢谢2楼3楼的同学
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-18 05:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表