I need to implement a listener in JavaScript by anonymous subclass of an existing abstract base class, define开发者_开发百科d like this:
public class Speaker {
public static abstract class MyListener {
private String name;
public MyListener(final String name) { this.name = name; }
public abstract boolean listen(final String words);
}
}
In java, implementing a listener is done with anonymous subclasses:
MyListener newListener = new MyListener("George") {
public boolean listen(final String words) throws Exception { Thread.sleep(500); }
}
If I try to do that in JavaScript, I can't call the constructor of the abstract base class and implement the virtual function at the same time.
It works if I remove the constructor argument, then a call to MyListener() with the method implementation as a parameter creates an instance of the subclass I need. But I actually need to call the constructor with both the constructor parameter and the method implementation.
Maybe this helps:
http://download.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html#jsimplement
It's about implementing interfaces, but might be applicable.
精彩评论