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

查看:14
本文介绍了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());
    }
}

我可以做这样的事情吗?

Can I do something like this ?

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.

与成员类一样,出于同样的原因,本地类不能包含静态字段、方法或类.唯一的例外是声明为 static 和 final 的常量.

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.

如前所述,局部类可以使用局部变量、方法参数,甚至是在其作用域内的异常参数,但前提是这些变量或参数被声明为final.这是因为本地类的实例的生命周期可能比定义该类的方法的执行时间长得多.出于这个原因,本地类必须拥有它使用的所有本地变量的私有内部副本(这些副本由编译器自动生成).确保局部变量和私有副本始终相同的唯一方法是坚持局部变量是最终的.

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天全站免登陆