向超类传递参数
当调用超类的方法时,括号是很重要的!如果参数列表为空,并且没有使用括号,那么所有的参数都将传递给超类。但是,参数列表为空,并且使用了括号时,将不会给超类传递任何参数。
super_args.rb
# This passes a, b, c to the superclass
def initialize(a, b, c, d, e, f)
super(a, b, c)
end
# This passes a, b, c to the superclass
def initialize(a, b, c)
super
end
# This passes no arguments to the superclass
def initialize(a, b, c)
super()
end
要更好的了解
super
关键字的使用,请参阅本章末尾的深入探索部分。