1. Functional programming has three parts:
The first part is: Lambda expression
The second part is: method reference
The third part is: functional interface
I just came into contact with Lambda expression, I think it is amazing, it can replace the traditional programming method with short code
Take a simple chestnut:
We want to implement a method in an interface: the first way is: first define a class to implement the interface, rewrite its method, then instantiate the object of this class, and call this method;
The second way is: through the way of inner class
Now it’s time for Lambda to shine:
First define a static method, the formal parameter is the interface, and then call the method of the interface in the static method
The premise of using Lambda expression is: there must be an interface, and there is only one method in the interface
If there is only one execution statement in Lambda, {} and ; can be omitted
Code content:
public interface Eatable {
void eat();
}
public class Eattest implements Eatable{
@Override
public void eat() { System.out.println("I like to eat roast chicken"); }
public class Test2 {
public static void main(String[] args) { // Realize Eatable by instantiating classes and polymorphism e=new Eattest(); eatdemo(e); // Realize eatdemo(new Eatable() by internal classes ) { @Override public void eat() { System.out.println("I like to eat fried chicken"); } }); // Implement eatdemo through Lambda (() ->{ System.out.println("I like to eat fried chicken so much "); }); //Equivalent to eatdemo(()->System.out.println("I like fried chicken")); } private static void eatdemo(Eatable e) { e.eat(); } }
The second part of Lambda: the omission of writing
1.1 You can omit parameter types such as:
flytest((String s) ->{ System.out.println(s); }); // parameter type can be omitted flytest((s) ->{ System.out.println(s); });
1.2 If the parameter type is to be omitted, all parameter types must be omitted
for example:
addtest((x,int y) ->{
return x+y;
});
This way of writing is wrong and will report an error
Both must be omitted: addtest((x,y) ->{ return x+y; });
2.1 If there is only one parameter, you can omit ()
flytest(s->{
System.out.println(s);
});
2.2 If there is only one statement in the code block, {} and ; can be omitted
flytest(s->System.out.println(s));
3. If there is only one statement in the code block, {} and can be omitted; if there is return, it can be omitted
addtest((x,y) ->x+y);
Code demo:
//addition interface
public interface Add {
int add(int x,int y);
}
//behavior action interface
public interface Flyable {
void fly(String s);
}
implemented in the main method
public class Show {
public static void main(String[] args) { flytest((String s) ->{ System.out.println(s); }); // parameter type can be omitted flytest((s) ->{ System.out. println(s); }); //If you want to omit parameter types, all parameter types must be omitted // Wrong // addtest((x,int y) ->{ // return x+y; // } ); //If there is only one parameter, you can omit () flytest(s->{ System.out.println(s); }); //If there is only one statement in the code block, you can omit {} and ; flytest (s->System.out.println(s)); //If there is only one statement in the code block, {} and can be omitted; if there is return, it can be omitted
addtest((x,y) ->x+y);
}
private static void flytest(Flyable f) { f.fly("That kind of bird is flying in the sky!"); } private static void addtest(Add a) { int sum=a.add(50, 100); System.out.println(sum); } }
thanks for watching!