我找到了以下作業解決方案(鏈接 1、鏈接 2),它們呼叫了祖父方法但沒有任何引數。有誰知道如何使用引數呼叫祖父方法?
class GrandParent
def fun(word)
puts word
end
end
class Parent < GrandParent
def fun(word)
puts word 'end'
end
end
class Child < Parent
def fun(word)
GrandParent.instance_method(:fun).bind(self).call
end
end
uj5u.com熱心網友回復:
您可以像這樣直接傳遞引數來呼叫:
class Child < Parent
def fun(word)
GrandParent.instance_method(:fun).bind(self).call(param1, param2)
end
end
uj5u.com熱心網友回復:
call接受引數
GrandParent.instance_method(:fun).bind(self).call word
我不知道您的用例,但這是一個非常糟糕的主意?。Child它直接在和之間創建不必要的依賴關系,GrandParent因此各種通常合理的重構都會導致崩潰,例如移動fun到僅在 中實作Parent,更改Parent為子類不同但相似的父級等。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/437504.html
標籤:红宝石
上一篇:Sorbet覆寫來自內核的URI
