我一直在做一些'猴子補丁'(ahem請原諒我超人補丁),像這樣,將下面的代碼和更多的文件添加到文件中,在我"#{Rails.root}/initializers/"
文件夾:繼承自**其他**類,而不是實際的父類
module RGeo
module Geographic
class ProjectedPointImpl
def to_s
coords = self.as_text.split("(").last.split(")").first.split(" ")
"#{coords.last}, #{coords.first}"
end# of to_s
def google_link
url = "https://maps.google.com/maps?hl=en&q=#{self.to_s}"
end
end# of ProjectedPointImpl class
end# of Geographic module
end
我最終意識到,有兩個不同的_Point_
情況下,我想利用這些方法(它們都具有相同的格式,即熟知文本(WKT)字符串)並將上述兩種方法的精確副本添加到某個RGeo::Geos::CAPIPointImpl
類空間中。
我的話,在我年輕,沒有經驗的方式,想着幹後(不要重複自己)的原則,着手創建一個特設類,我認爲我也許可以從兩個
繼承class Arghhh
def to_s
coords = self.as_text.split("(").last.split(")").first.split(" ")
"#{coords.last}, #{coords.first}"
end# of to_s
def google_link
url = "https://maps.google.com/maps?hl=en&q=#{self.to_s}"
end
end
,並告訴我的課,從它繼承,即:ProjectedPointImpl < Arghhh
我被及時迴應了紅寶石這個錯誤,當我停下來,然後嘗試重新加載我的rails控制檯:
`<module:Geos>': superclass mismatch for class CAPIPointImpl (TypeError)
...
我覺得我的天真在試圖讓CAPIPointImpl(在這種情況下),以繼承另一個類比其父也亮點關於這個問題我的知識差距非常明確
我可以使用什麼方法實際上將額外的共享方法嫁接到來自其他獨立父母的兩個類上? ruby是否允許這些類型的抽象異常?
Ruby不支持多重繼承。如果您嘗試重新打開一個已經定義的具有繼承性但帶有不同父級的類,那麼您將得到您發佈的錯誤。去看看模塊,看看理查德庫克下面說什麼,你應該能夠得到你想要的。 – xaxxon