public String name;
public String getName(){
return name;
}
public void doSomething() {
boolean getName = (boolean) MyBean::getName;
System.out.println(getName);
}
}
如何得到 MyBean::getName这个类型
Assistant
In Java, MyBean::getName is a method reference. The type of a method reference in Java depends on the context in which it is used, typically involving functional interfaces. For a method reference to MyBean::getName, the appropriate functional interface would be one that matches the signature of the method getName(). In this case, since getName() returns a String and takes no arguments, it would match the Supplier<String> interface from java.util.function.
Here's a breakdown of the steps you can take to obtain and use the type of MyBean::getName:
Determine the Functional Interface: In this case, since getName() returns a String and takes no arguments, the functional interface is Supplier<String>.
Implement the Method Reference in Code:
java
import java.util.function.Supplier;
public class MyBean {
public String name;
public String getName(){
return name;
}
public void doSomething() {
// Use MyBean::getName as Supplier<String>
Supplier<String> nameSupplier = this::getName;
// Now invoke get() to get the name
String nameValue = nameSupplier.get();
System.out.println(nameValue);
}
public static void main(String[] args) {
MyBean myBean = new MyBean();
myBean.name = "Sample Name";
myBean.doSomething(); // Output should be "Sample Name"
}
}
Explanation:
Method Reference Syntax: this::getName refers to the instance method getName() of the current object this. In a static context, MyBean::getName would refer to a static method, but in this case, it is an instance method.
Functional Interface: Supplier<String> is used because the getName() method matches a Supplier's abstract method get(), which also returns a String and takes no arguments.
Assignment and Execution: The method reference this::getName is assigned to the Supplier<String> variable nameSupplier, and then you can call nameSupplier.get() to execute the method and retrieve the name.
This approach allows you to handle method references in Java by associating them with the appropriate functional interfaces.
User
Supplier nameSupplier = this::getName;为什么可以这样写?
Assistant
The line Supplier<String> nameSupplier = this::getName; is an example of using a method reference, which is a feature introduced in Java 8. Method references provide a way to refer to methods without invoking them, allowing you to treat methods as first-class expressions. This makes it convenient to pass methods as arguments, use them for deferred execution, or store them in variables.
Here's a breakdown of why and how this works:
Why This Works:
Functional Interface Compatibility:
Supplier<T> is a functional interface in the java.util.function package. It has a single abstract method get() which returns a result of type T and takes no arguments.
The method getName() in your MyBean class matches this pattern: it takes no arguments and returns a String.
Method References:
this::getName is a method reference. This syntax is a shorthand for using a lambda expression that would look like () -> this.getName().
Java can automatically use method references where a functional interface is expected, given that the method's signature matches the interface's single abstract method.
Type Inference and Assignment:
When you assign a method reference to a variable, Java checks that the method reference matches the required method signature of the functional interface.
In this case, getName() corresponds to the Supplier<String> interface because they both return a String with no parameters.
How It Works:
Declaration and Assignment: Supplier<String> nameSupplier = this::getName;
Supplier<String> specifies the type of functional interface.
this::getName tells Java to refer to (but not execute) the getName method of the current instance (this).
Usage:
When you call nameSupplier.get(), it internally calls getName() on the instance referred to by this, and it returns the value.
Key Benefits:
Conciseness: Method references provide a more concise way to express what would otherwise be lambda expressions.
Readability: Using method references can make your code cleaner and easier to read, as it is often more straightforward than the equivalent lambda.
Reusability: You can pass method references around like any other variable, allowing flexible use in different contexts requiring the same logical behavior.
This feature is part of Java's adoption of functional programming concepts, enabling developers to write more expressive and less boilerplate-heavy code.