TL;博士:super
的行为以意想不到的方式和变量的事情,而不仅仅是对象。
当super
被调用,它不是所谓与中传递的对象。
这就是所谓的与在通话的时候叫options
变量。例如,用下面的代码:
class Parent
def to_xml(options)
puts "#{self.class.inspect} Options: #{options.inspect}"
end
end
class OriginalChild < Parent
def to_xml(options)
options.merge!(:methods => [ :murm_case_name, :murm_type_name ])
super
end
end
class SecondChild < Parent
def to_xml(options)
options = 42
super
end
end
begin
parent_options, original_child_options, second_child_options = [{}, {}, {}]
Parent.new.to_xml(parent_options)
puts "Parent options after method called: #{parent_options.inspect}"
puts
OriginalChild.new.to_xml(original_child_options)
puts "Original child options after method called: #{original_child_options.inspect}"
puts
second_child_options = {}
SecondChild.new.to_xml(second_child_options)
puts "Second child options after method called: #{second_child_options.inspect}"
puts
end
它产生输出
Parent Options: {}
Parent options after method called: {}
OriginalChild Options: {:methods=>[:murm_case_name, :murm_type_name]}
Original child options after method called: {:methods=>[:murm_case_name, :murm_type_name]}
SecondChild Options: 42
Second child options after method called: {}
可以看到,与SecondChild
超级方法被调用与可变options
它指的值42
一个Fixnum
,而不是与options
最初提到的对象。
随着使用options.merge!
,你会修改传递给你的哈希对象,这意味着该对象称为由可变original_child_options
现在修改,如可以在Original child options after method called: {:methods=>[:murm_case_name, :murm_type_name]}
线可以看出。
(注:我在SecondChild改变options
到42,而不是调用Hash#merge
,因为我想证明这不是在物体上的副作用仅仅是一种情况)
谢谢你的好例子。如果我将在OriginChild和SecondChild的选项之前写入超级,那么它们会有什么不同。 – 2010-11-16 13:56:56
@Krunal:你可以修改上面的代码,然后在irb(Interactive Ruby Shell)中重新运行它。你听说过irb,对吧? – 2010-11-16 22:30:49