访问匿名或本地内部类中的匿名或本地内部类
好的,所以我知道如何访问包含内部类的外部类,无论是匿名的还是内部的。
但我的问题是,如果外部类本身是内部类,如何访问它?一些有帮助的代码:
public final class LocationPage extends BasePage {
private static final String CRITERIA_FORM_ID = "CriteriaForm";
protected Panel onCreateBodyPanel() {
return new ViewBodyPanel(BasePage.BODY_PANEL_ID) {
public void invokeMe() {
// How do I Invoke This Method?
}
private Form<CriteriaBean> helpCreateCriteriaForm() {
return new Form<CriteriaBean>(LocationPage.CRITERIA_FORM_ID) {
@Override
protected void onSubmit() {
LocationPage.this.ViewBodyPanel.invokeMe(); // Compile Error.
}
};
}
};
}
}
更新:对于那些想了解我在这里尝试做什么的人,这里有一个完整的代码示例。这实际上是 Apache Wicket 特有的,但我认为您可以明白这个想法。看一下名为 onSubmit 的方法。我添加了代码注释来帮助查明它。
更新二:使代码示例切中要点。对此感到抱歉!
Okay, so I know how to access the outer class that encloses an inner class, whether its anonymous or inner.
But my question is, how to access the outer class if it itself is an inner class? Some code to help:
public final class LocationPage extends BasePage {
private static final String CRITERIA_FORM_ID = "CriteriaForm";
protected Panel onCreateBodyPanel() {
return new ViewBodyPanel(BasePage.BODY_PANEL_ID) {
public void invokeMe() {
// How do I Invoke This Method?
}
private Form<CriteriaBean> helpCreateCriteriaForm() {
return new Form<CriteriaBean>(LocationPage.CRITERIA_FORM_ID) {
@Override
protected void onSubmit() {
LocationPage.this.ViewBodyPanel.invokeMe(); // Compile Error.
}
};
}
};
}
}
UPDATE: For those wanting to see what I am trying to do here, here is a complete code sample. This is actually Apache Wicket specific but I think you can get the idea. Have a look at a method named onSubmit. I added a code comment to help pinpoint it.
UPDATE TWO: Made the code sample to the point. Sorry about that!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你只需要指定ParentClass.this.something来消除歧义。如果您的 Form 没有方法 invokeMe,您可以简单地使用名称而不进行限定,编译器应该找到它:
如果该函数也存在于内部类中,那么 Java 中没有任何技巧可以做到这一点。而是将 ViewBodyPanel.invokeMe 方法重命名或包装为明确的内容。
You only need to specify ParentClass.this.something to disambiguate. If your Form does not have a method invokeMe, you can simply use the name without qualifying, and the compiler should find it:
If the function does exist in the inner-inner class as well, there's no trick in Java to do it. Rather rename or wrap your ViewBodyPanel.invokeMe method into something that is unambiguous.
如果您使用内部类,则可以执行以下操作
If you are using inner classes than you can do the following