Project1

标题: super出错 [打印本页]

作者: hys111111    时间: 2017-1-13 09:54
标题: super出错
RUBY 代码复制
  1. class Game_Bicycle < Game_Character
  2.  
  3.   def initialize(id=0)
  4.     super
  5.     @id = id
  6.   end
  7.  
  8. end

上面这个脚本,当执行Game_Bicycle.new之后,第4行super出错。
可是Game_Character这个类下的initialize方法是没有像(id=0)这样的括号的啊。

需要加括号的时候,应该像下面这样,因为Window_Base的initialize的方法有括号(x,y,w,h)才会加。
RUBY 代码复制
  1. class Window_XXX < Window_Base
  2.  
  3.   def initialize
  4.     super(0,0,192,160)
  5.     ……
  6.   end
  7.  
  8. end


那这里该怎么改?
作者: RyanBern    时间: 2017-1-13 10:15
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()




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1