The first code example is called anonymous inner class.
Both examples are valid in Java. The method is just interested in ListenerForX
and doesn't care about the implementation thereof.
Domanda
I have a problem with naming following situation
{...}
X.a;
a.addListener( new ListenerForX(){
// some interface methods
});
{...}
It is the same as:
{...}
X.a;
a.addListener( new XListener());
{...}
private class XListener implements ListenerForX{
//some methods
}
How is it called?
Soluzione
The first code example is called anonymous inner class.
Both examples are valid in Java. The method is just interested in ListenerForX
and doesn't care about the implementation thereof.
Altri suggerimenti
The first one is an anonymous inner class.
Say you have an interface Walkable
with doWalk ()
,
For a method that takes in a Walkable instance you could write something like .
takeForaWalk(new Walkable() {
void doWalk() {
//Impl
}}
)