向超类传递参数

当调用超类的方法时,括号是很重要的!如果参数列表为空,并且没有使用括号,那么所有的参数都将传递给超类。但是,参数列表为空,并且使用了括号时,将不会给超类传递任何参数。

super_args.rb
  1. # This passes a, b, c to the superclass
  2. def initialize(a, b, c, d, e, f)
  3. super(a, b, c)
  4. end
  5. # This passes a, b, c to the superclass
  6. def initialize(a, b, c)
  7. super
  8. end
  9. # This passes no arguments to the superclass
  10. def initialize(a, b, c)
  11. super()
  12. end
要更好的了解 super 关键字的使用,请参阅本章末尾的深入探索部分。