Learning notes: Because Java anonymous classes learn some small uses of the interface

Source: Internet
Author: User

A lot of interesting code was found while looking at COMETD's sample code, but to be honest, it was really tiring to look at someone else's code, so just see this knowledge point and make a note. Take a look at the code: Code 1 There is a new operation in this code, and it is done in the method parameters, which gives me a curious point. At this point, the first thought is the anonymous class in C #, so looking for information on the Internet, is really similar grammatical characteristics. Continue to look at the code to find a problem:Configurableserverchannelis an interface, andInitializeris an internal nested interface in the Configurableserverchannel. Code 2 There is another problem, how can interfaces be instantiated? Code in code 1, found that it was instantiated with a direct implementation, and implemented the ConfigureChannel method. This means that a class is implemented directly to inherit the initializer interface. Here the anonymous class is on display. This means that the code appears to be instantiating an interface, actually implementing a class through the mechanism of an anonymous class, and dynamically creating an instance of the anonymous class. The benefits of this writing are obvious, using interfaces to contract, and then actually using the business code when it is used. This makes the code much leaner, without the need to create a bunch of Java code units.  However, if the class needs to be named and reused, it is less appropriate to use this approach, or there will be a lot of duplicated code. When looking at the code to learn another knowledge, is the original interface can also be implemented, see Code 2 in the persistent static class is directly inherited implementation of the initializer interface. This way of writing for the first time, think for a while not too understand how to go. So I wrote an example of running around: Code 3 The above code results in the following: I am object Instance:ai am staticclass:b I went, OK, a little calm. The original static class can also be instantiated!!!!!! In order to confirm this problem I take Staticclass's static to remove, the result of the operation is the same, then I do not know this static has what concrete function? It's time to study and study.

Learning notes: Because Java anonymous classes learn some small uses of the interface

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.