我一直在做一些'猴子补丁'(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