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