Lamda expression
- λ It is the eleventh letter in the Greek alphabet, and its English name is Lambda.
- Why use lambda expressions:
1. Avoid too many anonymous inner class definitions.
2. It can make your code look very concise.
3. Remove a pile of meaningless code, leaving only the core logic. - lambda expressions are essentially functional programming concepts. Definition of functional interface:
1. Any interface that contains only one abstract method is a functional interface.
2. For functional interfaces, we can create objects of the interface through lambda expressions.
lamda expression syntax: (method parameter table) - > {method body}
Parentheses can be omitted when there is only one parameter, and braces can be omitted when there is only one statement in the function body.
Lambda expression introduces an operator - >, called lambda operator or arrow operator, into the Java language, which divides lambda into two parts:
Left: all parameters required by Lambda expression are specified
Right: the Lambda body is defined, that is, the function to be performed by the Lambda expression.
[e.g.]
We know that the Thread class inherits the Runnable interface.
View that the Runnable interface has only one abstract method run(), so the Runnable interface is a functional interface. We can create the object of the interface through lambda expression: new thread (() - > system. Out. Println ("multithreading learning"). start();
Inner class and lamda expression:
package Threads; public class Testlambda { //3. Static internal class static class Like2 implements ILike { @Override public void lambda() { System.out.println(" i like lambda2"); } } public static void main(String[] args) { ILike like = new Like(); like.lambda(); like = new Like2(); like.lambda(); //4. Local internal class class Like3 implements ILike { @Override public void lambda() { System.out.println(" i like lambda3"); } } like = new Like3(); like.lambda(); //5. Anonymous inner class like = new ILike() { @Override public void lambda() { System.out.println(" i like lambda4"); } }; like.lambda(); //6. Use lambda expression like = ()->{ System.out.println(" i like lambda5"); }; like.lambda(); } } //1. Define a functional interface interface ILike { void lambda(); } //2. Implementation class class Like implements ILike { @Override public void lambda() { System.out.println(" i like lambda"); } }
Static proxy
- Why use a static proxy:
Proxy objects can do many things that cannot be done by objects; Real objects focus on doing their own things. - Static agent summary:
1. Both real objects and proxy objects should implement the same interface
2. The proxy object should represent the real role
Case: wedding company and wedding
package Threads; public class StacticProxy { public static void main(String[] args) { //Deliver real objects to the agency WeddingCompany weddingCompany = new WeddingCompany(new You()); weddingCompany.HappyMarry(); } } //Jointly implemented interface interface Marry { void HappyMarry(); } //Real character, you get married class You implements Marry { @Override public void HappyMarry() { System.out.println("The bride and groom are married"); } } //Acting role to help you get married class WeddingCompany implements Marry { //Who to represent -- the real target role of private Marry target; //Constructor, transfer is really an object public WeddingCompany(Marry target) { this.target = target; } @Override public void HappyMarry() { before(); this.target.HappyMarry();//Real object after(); } //Functions implemented by agent private void after() { System.out.println("Agent company to clean up garbage"); } private void before() { System.out.println("The agent arranges the site"); } }