查看: 2696|回复: 2

[已经解决] super出错

[复制链接]

双子人

梦石
0
星屑
3205
在线时间
3619 小时
注册时间
2009-4-4
回帖
3901

开拓者

发表于 2017-1-13 09:54:57 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

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

×
[pre lang="ruby"]class Game_Bicycle < Game_Character
  
  def initialize(id=0)
    super
    @id = id
  end
  
end[/pre]
上面这个脚本,当执行Game_Bicycle.new之后,第4行super出错。
可是Game_Character这个类下的initialize方法是没有像(id=0)这样的括号的啊。

需要加括号的时候,应该像下面这样,因为Window_Base的initialize的方法有括号(x,y,w,h)才会加。
[pre lang="ruby"]class Window_XXX < Window_Base

  def initialize
    super(0,0,192,160)
    ……
  end

end[/pre]

那这里该怎么改?
梦石
0
星屑
9557
在线时间
5074 小时
注册时间
2013-6-21
回帖
3459

开拓者贵宾剧作品鉴家

发表于 2017-1-13 10:15:21 | 显示全部楼层
super calls the method of the parent class, if it exists. Also, as @EnabrenTane pointed out, it passes all the arguments to the parent class method as well.


翻译:super 会将该方法的参数原封不动地传给父类同名方法(如果有的话)。

你的 initialize 里面有 id = 0 这个默认参数,因此 super 会传一个额外的参数给父类的方法,但是实际上父类不需要这个参数,所以报错。

解决办法:将 super 改为 super()

点评

好吧,解决了  发表于 2017-1-13 11:07

评分

参与人数 1星屑 +200 收起 理由
hys111111 + 200 认可答案

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部