我想了解繼承:當我期待4
Ruby和繼承
moe = Car.new
larry = StretchLimo.new
當我做moe.wheel
,我得到6
,:
class Car
@@wheels = 4
def wheel
@@wheels
end
end
class StretchLimo < Car
@@wheels = 6
def whee
@@wheels
end
def turn_on_television
end
end
我實例,像這樣一些對象。
我遵循的Ruby教程說它應該是4
。 Larry.whee
顯然應該返回6
。
順便說一下,我添加了「wheel
」和「whee
」函數,所以我可以看到值。任何人都可以解釋這裏有什麼問題?
歡迎使用stackoverflow。請正確格式化您的代碼。 –
ruby中的類變量很奇怪和令人困惑。最好不要使用它們。 – levinalex