Java 7中的闭包

Swa*_*rma 99 java closures

什么是关闭?它应该包含在Java 7中.(已经讨论了封装包含在Java 7中,但最终没有包括在内.-ed)任何人都可以向我提供一些可靠的参考资料,我可以从中学习有关闭包的内容吗?

Tom*_*ine 82

闭包是一个代码块,可以通过访问封闭范围的变量来引用(和传递).

从Java 1.1开始,匿名内部类以高度冗长的方式提供了这种功能.它们还具有仅能够使用final(并且明确分配)局部变量的限制.(注意,即使是非final局部变量也在范围内,但不能使用.)

Java SE 8旨在为单方法接口*提供更简洁的版本,称为"lambdas".尽管一些细节随机变化,但Lambdas与匿名内部类具有相同的限制.

Lambda正在Project LambdaJSR 335下开发.

*最初设计更灵活,允许单抽象方法(SAM)类型.不幸的是,新设计不太灵活,但确实试图证明允许在接口内实现.


Bar*_*lla 57

这是Neal Gafter的博客,它是在Java中引入闭包的先驱之一.他在2007年1月28日关闭的帖子被命名为闭包定义在他的博客上有很多信息可以帮助你开始和视频.这是一个很棒的Google演讲 - 编程语言高级主题 -与Neal Gafter的Java闭包.

  • 您是否拥有比整体博客更具体的链接? (2认同)
  • 试试这个http://gafter.blogspot.co.uk/2007/01/definition-of-closures.html (2认同)

小智 7

Tom Hawtin说

闭包是一个代码块,可以通过访问封闭范围的变量来引用(和传递).

现在我试图模仿维基百科上的JavaScript闭包示例,对Java 进行" straigth "转换,希望有用:

//ECMAScript
var f, g;
function foo() {
  var x = 0;
  f = function() { return ++x; };
  g = function() { return --x; };
  x = 1;
  print('inside foo, call to f(): ' + f()); // "2"  
}
foo();
print('call to g(): ' + g()); // "1"
print('call to f(): ' + f()); // "2"
Run Code Online (Sandbox Code Playgroud)

现在java部分:Function1是带有arity 1(一个参数)的"Functor"接口.Closure是实现Function1的类,这是一个充当函数(int - > int)的具体Functor.在main()方法中,我只是将foo实例化为Closure对象,复制来自JavaScript示例的调用.IntBox类只是一个简单的容器,它的行为类似于1 int的数组:

int a[1] = {0}

interface Function1   {
    public final IntBag value = new IntBag();
    public int apply();
}

class Closure implements Function1 {
   private IntBag x = value;
   Function1 f;
   Function1 g;

   @Override
   public int apply()  {
    // print('inside foo, call to f(): ' + f()); // "2"
    // inside apply, call to f.apply()
       System.out.println("inside foo, call to f.apply(): " + f.apply());
       return 0;
   }

   public Closure() {
       f = new Function1() {
           @Override
           public int apply()  {
               x.add(1);
                return x.get();
           }
       };
       g = new Function1() {
           @Override
           public int apply()  {
               x.add(-1);
               return x.get();
           }
       };
    // x = 1;
       x.set(1);
   }
}
public class ClosureTest {
    public static void main(String[] args) {
        // foo()
        Closure foo = new Closure();
        foo.apply();
        // print('call to g(): ' + g()); // "1"
        System.out.println("call to foo.g.apply(): " + foo.g.apply());
        // print('call to f(): ' + f()); // "2"
        System.out.println("call to foo.f.apply(): " + foo.f.apply());

    }
}
Run Code Online (Sandbox Code Playgroud)

它打印:

inside foo, call to f.apply(): 2
call to foo.g.apply(): 1
call to foo.f.apply(): 2 
Run Code Online (Sandbox Code Playgroud)

  • 除了这个名字,IMO与闭包无关 (2认同)

anu*_*ava 5

有关闭包的定义,请参阅此维基页面.

这个用于在Java 8中关闭的页面:http://mail.openjdk.java.net/pipermail/lambda-dev/2011-September/003936.html

另请参阅此问答:Java 7中的闭包


小智 5

Java 闭包将成为 J2SE 8 的一部分,并将于 2012 年底发布。

Java 8 的闭包支持包括 Lambda 表达式、方法引用、构造函数引用和默认方法的概念。

有关更多信息和工作示例,请访问:http://amitrp.blogspot.in/2012/08/at-first-sight-with-closures-in-java.html