Learning notes: Because the java Anonymous class learns some minor usage of interfaces, learning notes java

Source: Internet
Author: User
Tags java anonymous class

Learning notes: Because the java Anonymous class learns some minor usage of interfaces, learning notes java
I found a lot of interesting code when looking at the CometD sample code. But to be honest, it is really tiring to look at other people's code. So let's record this point. First look at a piece of code: Code 1 contains a new operation in the Code and is carried out in the method parameters, which gives me a curious point. At this point, I first thought of the anonymous class in C #, so I found the information on the Internet, which is indeed a similar syntax feature. After reading the code, you will find a problem: ableableserverchannel is an interface and Initializer is an internal nested interface in ableableserverchannel. Code 2 has another problem. How can an interface be instantiated? It is also the code in Code 1. It is found that it directly contains an implementation and implements the configureChannel method. This indicates that a class is directly implemented to inherit the Initializer interface. Here, the anonymous class is displayed.That is to say, the Code seems to be instantiating an interface. In fact, it implements a class through the Anonymous class mechanism and dynamically creates an instance of this anonymous class.The advantage of writing this is obvious. You can use the interface to agree and actually use the Business Code when using it. In this way, the code is much simpler, and you do not need to create a bunch of java code units. However, if the class needs to be named and used repeatedly, this method is not suitable. Otherwise, many duplicate codes will be generated. I learned another knowledge when reading the code, that is, the original interface can also be implemented. The Persistent static class in Code 2 directly inherits the Initializer interface. For the first time, I thought about it for a long time. So I wrote an example and ran it myself. I am object instance: AI am StaticClass: B. The result of the code above is as follows. Static classes can also be instantiated !!!!!! To confirm this problem, I have removed the static state class and the running result is the same. So I don't know what the static will do? I will study it later.

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.