Java uses anonymous class direct new interface

Source: Internet
Author: User

Look at the vector code, see this paragraph.

/**     * Returns An enumeration of the "this vector". The     * returned {@code Enumeration} object would generate all items in     * this vector. The first item generated is the item at index {@code 0},     * Then the item at index {@code 1}, and so on.     * * @return An enumeration of the "the" this  vector     *     @see     Iterator    */Public Enumeration<e> elements () {        return new enumeration<e> () {            int count = 0;            public boolean hasmoreelements () {                return count < Elementcount;            }            Public E nextelement () {                synchronized (vector.this) {                    if (count < Elementcount) {                        return Elementdata ( count++);                    }                }                throw new Nosuchelementexception ("Vector enumeration");}}        ;    }

 

Enumeration is an interface, and the internal definition is not described here.

The use of this new interface is used here, and if there are occasions when you just need to create an implementation class for an interface, the "tricks" above can be used to simplify the code.
In combination with the method set in the interface, you can achieve a powerful processing method, and save a lot of separate processing logic problems.

We look closely, we find that the implemented interface instance is not Elementcount, and the elementcount is defined in the vector, then if the method is called, returns an instance of the interface, how to obtain the value when the interface instance is used?
So write a demo to see under,

It is found that an instance of the instance member variable containing the V is pointed to in the returned instance. Therefore, this value can be used in the implementation of the method. (As for the exploration of how it is realized, readers who read this article can leave a message, thank you)

Then you would think whether returning an object would encapsulate one such point, and you could write it yourself in this way.

Car is an interface, in accordance with the above-mentioned mode of implementation, the existence of the above-mentioned reference; student not.

Summarize:

1, the appropriate time skillfully with the return interface instance mode, can be combined with a mode to achieve a function of simplification;

2, whether it can use its reference to deal with some problems, what kind of modifier member variables can be referenced object implementation, the purpose of this design considerations and so on, it is worth the time to think about the nerves.

Java uses anonymous class direct new 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.