This keyword with scala and anonymous functions/classes
In Java, I could reference the outer instance of a particular class: Sorry, I meant this.
object obj extends SomeOtherClass {
def myMethodOfSomeInstance = {
val uiThread = new SomeClass {
def run: Unit = {
chooser.showOpenDialog(SomeOtherClass.this)
}
开发者_高级运维 }
}
... This code does not compile, but on this line, I want to reference the parent instance? How do I do that?
chooser.showOpenDialog(SomeOtherClass.this)
You can use a self reference in the enclosing object that you can refer to:
object SomeObject { outer =>
def myMethodOfSomeInstance = {
val uiThread = new SomeClass {
def run: Unit = {
chooser.showOpenDialog(outer)
}
}
}
}
EDIT: By the way your declaration of the object would have to be object obj extends SomeOtherClass
for being valid scala code. You could then also refer to the enclosing object with obj.this
.
精彩评论