java中的方法可以嵌套吗,效果如何? [英] Can methods in java be nested and what is the effect?
本文介绍了java中的方法可以嵌套吗,效果如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,这是否合法:
class NAME {
method {
method {}
}
}
会有什么影响?是否涉及任何特殊语法?
and what would the effect be? is there any specialy syntax involved?
推荐答案
UPDATE由于 Java 8 方法可以使用 lambda 嵌套,请参阅这个其他问题.
UPDATE Since Java 8 methods can be nested using lambdas, see this other question.
此答案适用于 Java 8 之前的 Java 版本
This answer is valid for Java versions prior to Java 8
原答案如下:
java中的方法可以嵌套吗[...]?
不,这不可能.
你能得到的最接近的东西是:
The closest thing you can get is:
class Name {
void methodOne() {
class InnerClass {
void methodTwo() {
}
}
}
}
即在一个方法中定义的内部类中定义的第二个方法.
That is, a second method defined in a inner class defined in a method.
您可以在内部类中声明该方法为静态,这样您就不必调用new
You can declare the method static inside the inner class, so you do not have to call new
这篇关于java中的方法可以嵌套吗,效果如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文