1) what is the use of....
2) when to use......
3) why to use......
4) How to use..... (plz provide an example)
Here “ ...... „ represents “ -> and :: „
public class Button extends Actor { public void act() { if(Greenfoot.mouseClicked(this)) onClick(); } public void onClick() { System.out.println("Hello World!"); } }
public abstract class Button extends Actor { // ... public abstract void onClick();
public class HelloWorldButton extends Button { @Override public void onClick() { System.out.println("Hello World!"); } } public class StopButton extends Button { @Override public void onClick() { Greenfoot.stop(); } }
public interface Runnable { void run(); }
public class Button extends Actor { private Runnable onClick; public Button(Runnable onClick) { this.onClick = onClick; } public void act() { if(Greenfoot.mouseClicked(this)) onClick.run(); } }
Button stopButton = new Button(new Runnable() { @Override public void run() { Greenfoot.stop(); } });
Button stopButton = new Button(() -> Greenfoot.stop());
(a,b,c) ->
-> { // line of code // another line of code }
Button stopButton = new Button(Greenfoot::stop);
Button stopButton = () -> Greenfoot.stop();
Button stopButton = () -> Greenfoot.stop();
Runnable stopAction = () -> Greenfoot.stop(); // or Runnable stopAction = Greenfoot::stop;