试图理解Java 8中的方法参考(将类方法分配给功能接口) [英] Trying to understand method reference in Java 8 (assigning class method to functional interface)

查看:51
本文介绍了试图理解Java 8中的方法参考(将类方法分配给功能接口)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更好地理解方法引用的工作方式.在此示例中,我遵循代码的逻辑,但是我不理解执行此操作的价值.基本上,在STEP 2中为方法引用分配之后,将someMethod()用作MyInterface的display()抽象方法的实现.但是为什么呢?通过这样做,我们能获得什么?什么时候在架构上有意义?似乎我们采用了与该类无关的Interface并将它们与该分配相关联.

I am trying to better understand how method references work. In this example, I follow the logic of the code, but I don't understand the value of doing this. Basically, someMethod() is used as the implementation of the display() abstract method of MyInterface after the method reference assignment in STEP 2. But why? What are we gaining by doing this? When does this make sense architecturally? Seems that we take the Interface that has nothing to do with the class and associate them with this assignment.

@FunctionalInterface
interface MyInterface {
       void display();
}

class DerivClass {
    public void someMethod(){  
            System.out.println("Derived class method");  
    }
}

public class RefTest {

    public static void main(String[] args) {
        DerivClass dc = new DerivClass(); // STEP1: class instance
        MyInterface myInterf = dc::someMethod; // STEP 2: assign method ref to interface    
        myInterf.display(); // STEP 3: executes someMethod(), prints "Derived class method"
    }
}

推荐答案

开始理解方法参考的好地方是

Good place you to start understanding the method reference is the Oracle Documentation, that says:

您使用lambda表达式创建匿名方法.但是,有时lambda表达式除了调用现有方法外什么也不做.在这种情况下,通常更容易按名称引用现有方法.方法引用使您可以执行此操作;它们是紧凑的,易于阅读的lambda表达式,用于已经具有名称的方法.

You use lambda expressions to create anonymous methods. Sometimes, however, a lambda expression does nothing but call an existing method. In those cases, it's often clearer to refer to the existing method by name. Method references enable you to do this; they are compact, easy-to-read lambda expressions for methods that already have a name.

这样想:

存在方法定义(特定的主体,签名和返回类型);如果可以重用现有代码,为什么还要创建一个新的lambda表达式?让我们引用已经定义的方法并重用它."

"Method definition (particular body, signature and a return type) exists; why shall I create a new lambda expression if I can reuse existing code? let's refer to the already defined method and reuse it."


四种方法参考:

  1. SomeType :: staticMethodName -引用静态方法(您只需引用"SomeType"类的静态方法);

  1. SomeType::staticMethodName - reference to the static method (you just refer to the static method of "SomeType" class);

someTypeInstance :: instanceMethodName -对特定对象的实例方法的引用(在这里,您需要一个 actual 对象,从中可以获取方法引用);

someTypeInstance::instanceMethodName - reference to the instance method of a particular object (here, you need an actual object, from which you obtain the method reference);

SomeType :: instanceMethod -引用特定类型的任意对象的实例方法(在这种情况下,您无需显式创建对象);

SomeType::instanceMethod - reference to the instance method of an arbitrary object of a particular type (you do not need to explicitly create an object in this case);

SomeType :: new -引用构造函数(您只需引用构造函数).

SomeType::new - reference to the constructor (you simply refer to the constructor).


(2)特定对象的方法引用与(3)任意对象的方法引用之间的区别:

在前两个示例中,方法引用等效于提供方法参数的lambda表达式.例如:

In the first two examples, the method reference is equivalent to a lambda expression that supplies the parameters of the method. For example:

System.out::println == x -> System.out.println(x);
Math::pow == (x, y) -> Math.pow(x, y)

在第三种情况下,第一个参数成为方法的 target ,如:

In the third case, the first parameter becomes the target of the method, as in:

String::compareToIgnoreCase == (x, y) -> x.compareToIgnoreCase(y)

这篇关于试图理解Java 8中的方法参考(将类方法分配给功能接口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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