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