2011-01-08 27 views
41

這段代碼中的超級關鍵字是什麼?Ruby中的超級關鍵字

def initialize options = {}, &block 
    @filter = options.delete(:filter) || 1 
    super 
end 

據我所知,就像遞歸調用函數一樣,對吧?

回答

54

no ...超級調用父類的方法(如果存在)。另外,正如@EnabrenTane指出的那樣,它也將所有參數傳遞給父類方法。

+9

,默認情況下爲所謂的有通過給定的參數父功能以及 – EnabrenTane

+1

@Enabren很好的時候,我沒有提到。我將添加它 – sethvargo

+39

另外,如果你寫'super()'而不是'super',則沒有參數會被傳遞給父類的方法:http://bit.ly/hsQtfD – splicer

49

super使用相同的參數調用相同名稱的父方法。使用繼承類是非常有用的。

下面是一個例子:

class Foo 
    def baz(str) 
    p 'parent with ' + str 
    end 
end 

class Bar < Foo 
    def baz(str) 
    super 
    p 'child with ' + str 
    end 
end 

Bar.new.baz('test') # => 'parent with test' \ 'child with test' 

有沒有限制,你可以有多少次調用super,所以它可能有多個繼承的類使用它,像這樣:

class Foo 
    def gazonk(str) 
    p 'parent with ' + str 
    end 
end 

class Bar < Foo 
    def gazonk(str) 
    super 
    p 'child with ' + str 
    end 
end 

class Baz < Bar 
    def gazonk(str) 
    super 
    p 'grandchild with ' + str 
    end 
end 

Baz.new.gazonk('test') # => 'parent with test' \ 'child with test' \ 'grandchild with test' 

如果有沒有同名的父方法,但是,Ruby引發了一個異常:

class Foo; end 

class Bar < Foo 
    def baz(str) 
    super 
    p 'child with ' + str 
    end 
end 

Bar.new.baz('test') # => NoMethodError: super: no superclass method ‘baz’ 
+5

「超級調用父級方法名字,相同的論點。「一個* unadorned *'super'用相同的參數調用它們。沒有任何說孩子無法將參數的子集傳遞給父母,如果這是父母所做的。 Tinman: –

+0

:真夠的。 – vonconrad

+0

偉大的例子 - 感謝他們寫出來! – ckib16

16

super關鍵字可用於在調用類的超類中調用同名方法。

它將所有參數傳遞給父類方法。

超級不相同超級()

class Foo 
    def show 
    puts "Foo#show" 
    end 
end 

class Bar < Foo 
    def show(text) 
    super 

    puts text 
    end 
end 


Bar.new.show("Hello Ruby") 

引發ArgumentError:亞類內錯誤的參數數目(1 0)

超級(沒有括號)將調用父方法與恰好相同的參數傳遞給原方法(因此超級內部Bar#show變成超級(「Hello Ruby」)並導致錯誤,因爲父方法不會帶任何參數)

+0

非常好的解釋!謝謝! –

0

獎勵:

module Bar 
    def self.included base 
     base.extend ClassMethods 
    end 

    module ClassMethods 
     def bar 
      "bar in Bar" 
     end 
    end 
end 

class Foo 
    include Bar 
    class << self 
     def bar 
      super 
     end 
    end 
end 

puts Foo.bar # => "bar in Bar"