相关疑难解决方法(0)

Java 8中的实例方法引用类型之间有什么区别?

所以Java 8引入了方法引用,文档描述了这四种类型.

我的问题是两种实例类型之间的区别是什么?

  1. 引用特定对象的实例方法.
  2. 引用特定类型的任意对象的实例方法.

两者都参考参考但有什么显着不同?用于解决它们的类型推断是不同的吗?重要的是(在他们的例子中)一个是闭包而另一个是lambda?它与方法的参数数量有关吗?

java java-8 method-reference

20
推荐指数
1
解决办法
2621
查看次数

双冒号运算符不适用于 Java

只是在java中尝试了一些东西,发现了以下问题。

DefaultAndStaticMethodMain.java:8: error: not a statement
        implementation1::sendNotification;
        ^
1 error
Run Code Online (Sandbox Code Playgroud)

以下是我的代码。

父接口:

public interface ParentInterface {
    default void callForCompletion() {
        System.out.println("<<<< Notification sending completed. >>>>");
    }
}
Run Code Online (Sandbox Code Playgroud)

子界面:

public interface ChildInterface extends ParentInterface {
    public abstract void sendNotification();

    static String printNotificationSentMessage() {
        return "Notification is sent successfully.";
    }
}
Run Code Online (Sandbox Code Playgroud)

实施1:

public class Implementation1 implements ChildInterface {
    @Override
    public void sendNotification() {

        System.out.println("Implementation --- 1");
        System.out.println("Sending notification via email >>>");
    }
}
Run Code Online (Sandbox Code Playgroud)

实施2:

public class Implementation2 implements ChildInterface {
    @Override
    public …
Run Code Online (Sandbox Code Playgroud)

java

0
推荐指数
2
解决办法
259
查看次数

标签 统计

java ×2

java-8 ×1

method-reference ×1