java中的方法可以嵌套吗?效果如何?

发布于 2024-08-28 18:15:53 字数 110 浏览 5 评论 0原文

例如,这是否合法:

class NAME {
method {
     method {} 
}
} 

效果如何?是否涉及任何特殊语法?

for example and is this legal:

class NAME {
method {
     method {} 
}
} 

and what would the effect be? is there any specialy syntax involved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

瀞厅☆埖开 2024-09-04 18:15:53

更新
由于可以使用 lambda 嵌套 Java 8 方法,因此请参阅另一个问题

此答案对于 Java 8 之前的 Java 版本有效

原始答案如下:

java中的方法可以嵌套[...]吗?

不,这是不可能的。

您可以获得的最接近的东西是:

class Name {
    void methodOne() {
        class InnerClass {
           void methodTwo() {
           }
         }
     }
 }

即在方法中定义的内部类中定义的第二个方法。

您可以在内部类中将方法声明为静态,这样就不必调用 new

UPDATE
Since Java 8 methods can be nested using lambdas, see this other question.

This answer is valid for Java versions prior to Java 8

Original answer follow:

Can methods in java be nested[...]?

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

疑心病 2024-09-04 18:15:53

这是无效的语法;不支持此类功能。 (尽管 Java 7 正在考虑使用它)

相反,您可以使用嵌套类,您刚刚询问

That is invalid syntax; no such feature is supported. (Although it's being considered for Java 7)

Instead, you can use nested classes, which you just asked about.

甜心小果奶 2024-09-04 18:15:53

不。

一种解决方案只是将要调用的方法声明为“父”方法之外的私有方法 - 如果您真的很烦恼,可以使用某种命名约定来指示它们“属于”“父”方法。

您可以考虑的另一件事(似乎在程序员中并未广为人知)是您可以声明任何任意范围块并对其进行标记,然后使用break 来打破该块。

因此,以下内容是完全合法的 Java:

void method() {
      myInnerMethod : {
        // do some stuff

        if (condition) {
            break myInnerMethod;
        }

        // do some more stuff
      }
}

当然,作用域块并不是真正的方法,但在某些情况下,它可以用于执行您想要“内部方法”的操作。

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.

So the following is perfectly legal 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.

永言不败 2024-09-04 18:15:53

不,这是无效的语法。这真是一种耻辱——这是我想念艾达的事情之一。无法定义嵌套方法确实会在组织具有大量私有方法的类时产生很多问题。从那里开始,就会陷入缺乏凝聚力的泥沼。

您可以使用嵌套类,但它们是有代价的。每个嵌套类都会生成一个 $named 类。每个都在打开文件句柄(如果不是从存档中提取)以及其类定义占用的内存方面产生影响。有些系统对可以部署的文件数量(即生成的类总数)有上限(例如 Google Apps)。

换句话说,我不会使用嵌套类来模仿嵌套方法(以防万一您决定尝试这样做) 。)

现在,假设您可以使用嵌套方法(如在 Ada 中),这些方法将仅在封闭方法中可见(它们比典型的私有方法受到更多限制。)它们将是能够查看外部作用域中定义的变量和参数(但可能仅在定义为最终作用域时)。

它允许您在嵌套命名空间中组织方法和算法。当明智地使用时,嵌套方法确实有助于组织代码。

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.)

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 private methods.) They would be able to see variables and parameters defined in the outer scope (but probably only if defined as 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.

毁虫ゝ 2024-09-04 18:15:53

如果不进行检查,我会说编译器不接受这一点,因为此时需要在类中定义方法。但是,您可以在方法内定义内部类(请参阅“本地和匿名内部类”)。

http://java.sun.com/docs/books/教程/java/javaOO/innerclasses.html

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

懒猫 2024-09-04 18:15:53

Java 中不被接受。看起来你想进行递归,只需再次调用相同的方法即可。

method(){

  method();

}

Not accepted in Java. Looks like you want to do recursion, simply call the same method again.

method(){

  method();

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文