Java方法作为参数传递/调用

import java.util.function.Consumer;

class Printer {
    public void printMessage(String message) {
        System.out.println(message);
    }
}

public class MethodReferenceExample {
    public static void main(String[] args) {
        Printer printer = new Printer();

        // Method reference to an instance method
        Consumer<String> messagePrinter = printer::printMessage;

        // Call the referenced method through the functional interface
        messagePrinter.accept("Hello, world!");
    }
}

---from chatgpt answer

In Java, a method reference is a shorthand notation for referring to an existing method and using it as a parameter for passing around or invoking. Method references are often used in functional programming contexts, such as with Java 8's functional interfaces and the Streams API. They allow you to treat methods as first-class citizens, enabling concise and readable code.

There are different ways to use method references, but one common scenario is passing a method reference to another method that accepts a functional interface, and then invoking the referenced method through that functional interface. Let's break down the process step by step:

  1. Create a Functional Interface: You need a functional interface that matches the signature of the method you want to reference. A functional interface is an interface that has only one abstract method. For example, the java.util.function.Consumer interface represents an operation that accepts a single input argument and returns no result.

  2. Define the Method Reference: There are several ways to define a method reference depending on the method's context. Here's a general syntax:

    arduinoCopy code
    ClassName::methodName

    For instance, if you have a method printMessage in a class Printer, the method reference would be Printer::printMessage.

  3. Pass the Method Reference: Pass the method reference as an argument to a method that accepts a functional interface with a matching signature.

  4. Invoke the Referenced Method: Inside the method that receives the method reference, you can invoke the referenced method using the functional interface's method. This is essentially calling the method through the method reference.

Here's an example using a custom class:

import java.util.function.Consumer;

class Printer {
    public void printMessage(String message) {
        System.out.println(message);
    }
}

public class MethodReferenceExample {
    public static void main(String[] args) {
        Printer printer = new Printer();

        // Method reference to an instance method
        Consumer<String> messagePrinter = printer::printMessage;

        // Call the referenced method through the functional interface
        messagePrinter.accept("Hello, world!");
    }
}

In this example, Consumer<String> is a functional interface that takes a single parameter of type String and doesn't return any value. The printer::printMessage method reference matches this signature, so it can be assigned to the messagePrinter functional interface. The accept method of Consumer is then used to invoke the printMessage method through the method reference.

Remember that method references simplify the code by reducing boilerplate and improving readability, especially when working with functional interfaces and streams.

猜你喜欢

转载自blog.csdn.net/Goals1989/article/details/132361052