Java 8中使用非静态方法的lambda [英] lambda with non-static methods in Java 8

查看:883
本文介绍了Java 8中使用非静态方法的lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在新的Java 8中学习lambdas。有一件有趣的事情。如果method与功能接口具有相同的签名,则可以使用lambdas API将其分配给它。例如。

I am trying to learnd lambdas in new Java 8. There is one interesting thing. If method has the same signature as functional interface, it can be assigned to it using lambdas API. For instance.

Comparator<Integer> myComp = Integer::compare;

此方法(Integer.compare)是静态的,取两个值,一切都很完美。签名与接口方法中的签名相同。但这可以用非静态方法制作,例如

This method(Integer.compare) is static, takes two values, everything is perfect. the signature the same as in interface method compare. BUT this can be made with non-static methods, for example

Comparator<Integer> myComp = Integer::compareTo.

此方法是非静态的(实例级别),此外,它只需要一个值。据我所知,Java中没有非静态方法,每个方法都是静态的,但如果没有标记为静态,则需要这个作为第一个参数。如下所示

This method is non-static (instance level) and furthermore, it takes only one value. As I have understood, there is no non-static methods in Java, every method is static but if it is not marked as static it takes this as the first parameter. As following

compareTo(this,Integer value).

假设由于比较对象和整数而导致结果未定义是合理的。但是这样做。

It would be reasonable to assume that result will be undefined because of comparing object and integer. BUT THIS WORKS.

Comparator<Integer> comparator = Integer::compareTo;
Comparator<Integer> comparator2 = Integer::compare;
System.out.println(comparator.compare(1,2));
System.out.println(comparator2.compare(1,2));

这同样有效。
我调试了调用方法堆栈。在不创建实例的情况下调用比较器对象的方法比较时,这个。值已经由第一个参数初始化,当然这是对象的有效引用。

This works equally. I have debugged call method stack. While calling method compare of comparator object without creating instance, this. Value is already initialized by the first parameter and of course this is valid reference to the object.

所以问题是这是如何工作的?在调用方法编译器检查时,如果类只有一个字段与方法中的第一个param具有相同的类型,如果类有编译器隐含地创建具有初始化字段的类的新实例或者它是如何工作的?

SO the question is How does this work? While calling method compiler checks, if the class has only one field which has the same type as first param in method, if class has compiler implictly creates new instance of the class with initialized field or how does it work?

推荐答案

这是因为lambdas不是来自面向对象的世界。

This is because lambdas are not from Object Oriented world.

当您为 Comparator< Integer> 分配一些方法时,它的任务是执行比较。

When you assign some method to Comparator<Integer> it's task is to perform the comparision.

Comparator<Integer> methodOne = Integer::compare;
Comparator<Integer> methodTwo = Integer::compareTo;

methodOne.compare(1,2); 将被翻译为 Integer.compare(1,2)它被称为非实例捕获并引用静态方法

This methodOne.compare(1,2); will be translated to Integer.compare(1,2) it is called non-instance-capturing and refer to static method

methodTwo.compare(1,2); 将被翻译为 1.compareTo(2) is称为实例捕获并引用实例方法。

This methodTwo.compare(1,2); will be translated to 1.compareTo(2) is is called instance-capturing and refers to instance methods.

编译器知道您引用了哪种类型的方法,因此它可以无错误地处理。

Compiler "knows" what type of method you have referenced so it can process without error.

方法参考捕获

这篇关于Java 8中使用非静态方法的lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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