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?
推荐答案
更新 由于可以使用lambda嵌套Java 8方法,因此请参见其他问题.
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屋!
查看全文