App下載
首頁javalambdaJava Stream - 如何引用靜態(tài)和對(duì)象方法

Java Stream - 如何引用靜態(tài)和對(duì)象方法

我們想知道如何引用靜態(tài)和對(duì)象方法。
public class Main {

    public static void main(String[] args) {
        // MyClass class' static access
        Runnable r1 = Main::staticWork;

        // MyClass object's access
        Main myClass = new Main();
        Runnable r2 = myClass::work;
    }

    // Same method signature with Runnable's run method => void ***()
    public static void staticWork() {
        System.out.println("-- Static method body --");
    }

    // Same method signature with Runnable's run method => void ***()
    public void work() {
        System.out.println("-- Non-Static method body --");
    }


}