方法内的局部内部类的数量是否有限制?
我在处理一些 java 代码时遇到了一些困难
class foo{
public bar() {
class innerA {}
class innerB {} // Only this one is valid because it was declare last
}
}
我的问题:只有最后声明的内部类 (innerB) 在 foo::bar() 中可见。此外,我无法从另一个内部类中引用另一个内部类。示例:
innerB{
private innerA _a; // Error
}
我的问题:方法中可以拥有的本地内部类的数量是否有限制?本地内部类可以实例化其他本地内部类对象吗?他们应该吗?
编辑:我在 IDE 中输入错误,并且遇到了一些范围问题...再次感谢!
TIA, 小白
I'm having a bit of difficulty with some java code
class foo{
public bar() {
class innerA {}
class innerB {} // Only this one is valid because it was declare last
}
}
My Problem: Only the last declared inner class (innerB) is visible within foo::bar(). Additionally, I cannot reference either inner class from within the other. Example:
innerB{
private innerA _a; // Error
}
My Question: Is there some limit on the number of local inner classes you can have within a method? Can local inner classes instantiate other local inner class object? Should they?
EDIT: I miss-typed in my IDE and had some scoping issues... thanks again!
TIA,
noob
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请发布您真正尝试运行的代码,以及您从 Java 编译器获得的错误消息。您发布的代码不是有效的 Java。你想做的一切都是可行的。这是一个工作示例:
运行它给出:
Please post the code you really try to make work, and the error message you get from the Java compiler. The code you posted isn't valid Java. Everything you want to do is doable. Here's a working example :
Running it gives :
您忽略了为
bar()
指定返回类型。You neglected to specify a return type for
bar()
.