Java是否支持内部/本地/子方法? [英] Does Java support inner / local / sub methods?

查看:73
本文介绍了Java是否支持内部/本地/子方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码。

public class SubFunction {
    private String drawTribleX(){
        return trible("X");
    }
    private String trible(String t){
        return t + t + t;
    }
    public static void main(String[] args){
        SubFunction o = new SubFunction();
        System.out.println(o.drawTribleX());
    }
}

我可以这样做吗?

public class SubFunction {
    private String drawTribleX(){
        // *** move trible(t) inside drawTribleX() ***
        private String trible(String t){
            return t + t + t;
        }
        return trible("X");
    }
    public static void main(String[] args){
        SubFunction o = new SubFunction();
        System.out.println(o.drawTribleX());
    }
}

谢谢。

推荐答案

更新2014-02-09:

JDK 8 引入了lambdas(匿名函数表达式),允许你像这样解决它:

JDK 8 introduced lambdas (anonymous function expressions) which allow you to solve it like this:

Function<String, String> trible = s -> s+s+s;
System.out.println(trible.apply("X"));           // prints XXX






(JDK 7以下)

不,Java不支持直接嵌套方法。 (大多数函数式语言都可以,包括一些JVM语言,如Scala和Clojure!)

No, Java does not support "directly" nested methods. (Most functional languages do though, including some JVM languages such as Scala and Clojure!)

仅供参考;你可以定义本地类(方法中的类),这样 编译

Just for reference though; You can define local classes (classes within methods) so this does compile

class SubFunction {
    private String drawTribleX(){

        // *** move trible(t) inside drawTribleX() ***
        class Trible {
            private String trible(String t){
                return t + t + t;
            }
        }

        return new Trible().trible("X");
    }
    public static void main(String[] args){
        SubFunction o = new SubFunction();
        System.out.println(o.drawTribleX());
    }
}

注意虽然有一些对本地课程的限制


3.11.2。 对本地课程的限制

本地课程受以下限制:


  • 本地类仅在定义它的块中可见;它永远不能在该块之外使用。

  • A local class is visible only within the block that defines it; it can never be used outside that block.

本地类不能声明为public,protected,private或static。这些修饰符适用于类的成员;它们不允许使用局部变量声明或本地类声明。

Local classes cannot be declared public, protected, private, or static. These modifiers are for members of classes; they are not allowed with local variable declarations or local class declarations.

与成员类一样,出于同样的原因,本地类不能包含静态字段,方法,或者课程。唯一的例外是声明为静态和最终的常量。

Like member classes, and for the same reasons, local classes cannot contain static fields, methods, or classes. The only exception is for constants that are declared both static and final.

无法在本地定义接口。

本地类(如成员类)不能与其任何封闭类具有相同的名称。

A local class, like a member class, cannot have the same name as any of its enclosing classes.

如前所述,本地类可以使用局部变量,方法参数,甚至是其范围内的异常参数,但前提是这些变量或参数都是最终的。这是因为本地类实例的生命周期可能比定义类的方法的执行时间长得多。因此,本地类必须具有它使用的所有局部变量的私有内部副本(这些副本由编译器自动生成)。确保局部变量和私有副本始终相同的唯一方法是坚持局部变量是最终的。

As noted earlier, a local class can use the local variables, method parameters, and even exception parameters that are in its scope, but only if those variables or parameters are declared final. This is because the lifetime of an instance of a local class can be much longer than the execution of the method in which the class is defined. For this reason, a local class must have a private internal copy of all local variables it uses (these copies are automatically generated by the compiler). The only way to ensure that the local variable and the private copy are always the same is to insist that the local variable is final.

因此,正如您所看到的,在这些情况下,您的第一个选项(没有嵌套方法)更可取。

So, as you can see, your first option (without nested methods) is preferable in these situations.

这篇关于Java是否支持内部/本地/子方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆