Introduction and summary of Anonymous Classes and lambda Expressions in java (Anonymous Classes and Lambda Expressions), lambdaexpressions
2017/6/30
Reprinted written Source: http://www.cnblogs.com/daren-lin/p/anonymous-classes-and-lambda-expressions-in-java.html
In this document, I checked the collection introduction and introduced how to traverse a container by referring to an Aggregate operation (Aggregate Operations, I have never touched on it before, so I will learn more. To understand Aggregate Operations, we must review and summarize the two points mentioned in this article: Anonymous Classes and lambda Expressions ). Official documents are in English. To facilitate some keywords, you can use the original English document directly.
Anonymous class)
Anonymous class is similar to the local class, except for its no name, they can both define and instantiate it. Generally, if you only need to use one class once, you can use anonymous class.
The definition of anonymous class is somewhat different from that of local class. The definition of local class is a class definition, while the definition of anonymous class is an expression, therefore, you can define this class directly in the expression. The example below is a good example.
1 HelloWorld frenchGreeting = new HelloWorld() { 2 String name = "tout le monde"; 3 public void greet() { 4 greetSomeone("tout le monde"); 5 } 6 public void greetSomeone(String someone) { 7 name = someone; 8 System.out.println("Salut " + name); 9 }10 };
The expression of anonymous class consists of the following points:
1. new operator
2. The name of the extend class or implement interface. In this example, the Implement class has an interface called HelloWorld.
3. A pair of parentheses that contain parameters of the constructor. Because an interface is implemented here, no constructor or parameter is required.
4. A body
Note that because anonymous class is an expression, you must add a semicolon to the end;
Lambda Expressions)
It can be seen that anonymous class is much easier than redefining a class. Then, if there is only one method in a class, the definition looks a little complicated, and lambda expressions can make the situation more concise.
Refer:
Https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
Https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html