class Test
def initialize
@var = "125"
end
def testmethod
puts @var
puts "accessing me from child class"
end
end
class TestExtension < Test
def method1
puts @var = "One Hundred and twenty five"
testmethod()
end
end
t = Test.new
p = TestExtension.new
p.method1
t.testmethod
輸出:實例變量
One Hundred and twenty five
One Hundred and twenty five
accessing me from child class
125
accessing me from child class
我的問題是,訪問testmethod()
在子類TestExtension
結果在訪問這是在TestExtension
類被聲明的@var
是價值,而不是訪問值正在申報Test
班。這是對的嗎 ?
格式化你的代碼(通過把4個空格周圍的所有標識的所有代碼行和反引號前將英文文本)。閱讀此信息以獲取更多信息:http://stackoverflow.com/editing-help – Jakob 2010-12-13 07:55:50