Introduction and summary of Anonymous Classes and lambda Expressions in java (Anonymous Classes and Lambda Expressions), lambdaexpressions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.