是否有任何Java编程语言扩展可以创建嵌套函数?在许多情况下,我需要创建仅在另一个方法或for循环的上下文中使用一次的方法.到目前为止,我无法用Java实现这一点,即使它可以在Javascript中轻松完成.
例如,这不能在标准Java中完成:
for(int i = 1; i < 100; i++){
times(2); //multiply i by 2 and print i
times(i); //square i and then print the result
public void times(int num){
i *= num;
System.out.println(i);
}
}
Run Code Online (Sandbox Code Playgroud) 是否可以在Java中定义函数内的函数?我想做的事情如下:
public static boolean fun1()
{
static void fun2()
{
body of function.
fun2();
}
return returnValue;
}
Run Code Online (Sandbox Code Playgroud)
但我收到了错误Illegal start of expression.