java中的方法可以嵌套吗,效果如何?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2634328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can methods in java be nested and what is the effect?
提问by David
for example and is this legal:
例如,这是合法的:
class NAME {
method {
method {}
}
}
and what would the effect be? is there any specialy syntax involved?
会有什么影响?是否涉及任何特殊语法?
采纳答案by OscarRyz
UPDATESince Java 8 methods can be nested using lambdas, see this other question.
更新由于 Java 8 方法可以使用 lambda 嵌套,请参阅其他问题。
This answer is valid for Java versions prior to Java 8
此答案适用于 Java 8 之前的 Java 版本
Original answer follow:
原答案如下:
Can methods in java be nested[...]?
java中的方法可以嵌套[...]吗?
No, that's not possible.
不,那不可能。
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.
即在方法中定义的内部类中定义的第二个方法。
You can declare the method static inside the inner class, so you do not have to call new
您可以在内部类中声明静态方法,因此您不必调用 new
回答by SLaks
That is invalid syntax; no such feature is supported. (Although it's being considered for Java 7)
那是无效的语法;不支持此类功能。(虽然它正在考虑用于 Java 7)
Instead, you can use nested classes, which you just asked about.
相反,您可以使用刚刚询问的嵌套类。
回答by James P.
Without checking, I'd say that this isn't accepted by the compiler because as of this time methods need to be defined within a class. However, you could define an inner class inside the method (see "Local and Anonymous Inner Classes").
如果不检查,我会说这不被编译器接受,因为此时方法需要在类中定义。但是,您可以在方法内部定义一个内部类(请参阅“本地和匿名内部类”)。
http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
回答by Neil Coffey
No.
不。
One solution is just to declare the methods you want to call as private methods outside the "parent" method-- if you were really bothered, you could use some naming convention to indicate that they "belong" to the "parent" method.
一种解决方案是将您想要调用的方法声明为“父”方法之外的私有方法——如果您真的感到困扰,您可以使用一些命名约定来表明它们“属于”“父”方法。
Another thing that you could consider-- and doesn't appear to be widely known among programmers-- is that you can declare any arbitrary scope block and label it, then use break to break out of that block.
您可以考虑的另一件事——似乎在程序员中并不广为人知——是您可以声明任意范围的块并标记它,然后使用 break 来打破该块。
So the following is perfectly legal Java:
所以以下是完全合法的Java:
void method() {
myInnerMethod : {
// do some stuff
if (condition) {
break myInnerMethod;
}
// do some more stuff
}
}
Of course, the scope block is not really a method, but in some cases, it can be used to do what you'd want an "inner method" for.
当然,作用域块并不是真正的方法,但在某些情况下,它可用于执行您想要“内部方法”的操作。
回答by flopex
Not accepted in Java. Looks like you want to do recursion, simply call the same method again.
在 Java 中不被接受。看起来你想做递归,只需再次调用相同的方法。
method(){
method();
}
回答by luis.espinal
No. It is invalid syntax. And that's a shame - it is one of the things I miss from Ada. Not having the ability to define nested methods does create a lot of problems in organizing classes with substantial amounts of private methods. From there it's a slippery slope into lack-of-cohesion land.
不,这是无效的语法。这是一种耻辱——这是我想念艾达的事情之一。无法定义嵌套方法确实会在组织具有大量私有方法的类时产生很多问题。从那里是一个滑坡,进入缺乏凝聚力的土地。
You could use nested classes, but they come at a price. Each nested class generates a $named class. Each has a toll in terms of open file handles (if not pulled from an archive) as well memory taken by its class definition. Some systems have a cap on the number of files (thus total generated classes) that they can deploy (Google Apps for example.)
您可以使用嵌套类,但它们是有代价的。每个嵌套类生成一个 $named 类。每个都在打开文件句柄(如果不是从存档中提取)以及其类定义占用的内存方面造成损失。某些系统对它们可以部署的文件数量(因此生成的类总数)有上限(例如 Google Apps)。
In other words, I would not use nested classes to mimic nested methods (just in case you decide to try that.)
换句话说,我不会使用嵌套类来模拟嵌套方法(以防万一您决定尝试。)
Now, assuming that you could use nested methods (as in Ada), those methods would only be visible within the enclosing method (they'd be more restricted than typical privatemethods.) They would be able to see variables and parameters defined in the outer scope (but probably only if defined as final.)
现在,假设您可以使用嵌套方法(如在 Ada 中),这些方法将仅在封闭方法中可见(它们比典型的私有方法受到更多限制。)它们将能够看到定义在外部作用域(但可能仅当定义为 final 时。)
It would allow you to organize your methods and algorithms in nested namespaces. When used intelligently, nested methods really help in organizing your code.
它允许您在嵌套的命名空间中组织方法和算法。如果使用得当,嵌套方法确实有助于组织您的代码。