Hello. It's Kecho. ** Do you guys use lambda expressions? ** ** I will use it for the first time from now on. I'm an amateur.
First, the class that implements the Ruunable interface is as follows.
class lambda implements Runnable {
@Override
public void run() {
System.out.print("OK");
}
}
Implement the run method according to the interface and finish.
Runnable runner = new Runnable() {
@Override
public void run() {
System.out.print("OK-ish");
}
}; //OK-ish
Here the class is anonymized and defined in the same place as the instantiation. If you omit the last ";", a compile error will occur. Please let me know.
Redundant parts will be deleted by type inference.
Runnable runner = {
@Override
public void run() {
System.out.print("OK-ish");
}
}; //OK-ish
new runnable()
Can guess the type from the left side.
Runnable runner = {
{
System.out.print("OK-ish");
}; //OK-ish
public void run()
Since the runnable interface only has a run method, you don't have to specify it.
Runnable runner = () -> System.out.print("OK-ish");// OK-ish
Add the argument ``` ()->` `` and omit the {} to complete the lambda expression.
Is it possible to write like JS? ?? ??
Recommended Posts