我無法訪問內部類中外部類的實例變量。它是我使用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