我无法访问内部类中外部类的实例变量。它是我使用JRuby中创建一个简单的Swing应用程序:在内部类中访问外部类的私有成员:JRuby
class MainApp
def initialize
...
@textArea = Swing::JTextArea.new
@button = Swing::JButton.new
@button.addActionListener(ButtonListener.new)
...
end
class ButtonListener
def actionPerformed(e)
puts @textArea.getText #cant do this
end
end
end
唯一的解决办法我能想到的是这样的:
...
@button.addActionListener(ButtonListener.new(@textArea))
...
class ButtonListener
def initialize(control)
@swingcontrol = control
end
end
,然后使用@swingcontrol插件地方@textArea在“ actionPerformed'方法。
这是可能的java。那么为什么不在这里? – badmaash 2010-10-07 11:26:29
因为Ruby不是Java。范围规则是不同的。 – hallidave 2011-02-14 03:28:28