JavaScript Implementing Java Interfaces
The syntax for implementing a Java interface in JavaScript is similar to how
anonymous classes are declared in Java. You instantiate an interface and implement
its methods (as JavaScript functions) in the same expression. The following example
shows you how to implement the Runnable interface:
// Create an object that implements the Runnable interface by implementing
// the run() method as a JavaScript function
var r = new java.lang.Runnable() {
run: function() {
print("running...\n");
}
};
// The r variable can be passed to Java methods that expect an object implementing
// the java.lang.Runnable interface
var th = new java.lang.Thread(r);
th.start();
th.join();
If a method expects an object that implements an interface with only one method, you
can pass a script function to this method instead of the object. For instance, in the
previous example, the Thread() constructor expects an object that implements the
Runnable interface, which defines only one method. You can take advantage of
automatic conversion and pass a script function to the Thread() constructor instead of
the object. The following example shows you how you can create a Thread object
without implementing the Runnable interface:
// Define a JavaScript function
function func() {
print("I am func!");
};
// Pass the JavaScript function instead of an object that implements
// the java.lang.Runnable interface
var th = new java.lang.Thread(func);
th.start();
th.join();
You can implement multiple interfaces in a subclass by passing the relevant type
objects to the Java.extend() function; see Extending Concrete Java Classes.
anonymous classes are declared in Java. You instantiate an interface and implement
its methods (as JavaScript functions) in the same expression. The following example
shows you how to implement the Runnable interface:
// Create an object that implements the Runnable interface by implementing
// the run() method as a JavaScript function
var r = new java.lang.Runnable() {
run: function() {
print("running...\n");
}
};
// The r variable can be passed to Java methods that expect an object implementing
// the java.lang.Runnable interface
var th = new java.lang.Thread(r);
th.start();
th.join();
If a method expects an object that implements an interface with only one method, you
can pass a script function to this method instead of the object. For instance, in the
previous example, the Thread() constructor expects an object that implements the
Runnable interface, which defines only one method. You can take advantage of
automatic conversion and pass a script function to the Thread() constructor instead of
the object. The following example shows you how you can create a Thread object
without implementing the Runnable interface:
// Define a JavaScript function
function func() {
print("I am func!");
};
// Pass the JavaScript function instead of an object that implements
// the java.lang.Runnable interface
var th = new java.lang.Thread(func);
th.start();
th.join();
You can implement multiple interfaces in a subclass by passing the relevant type
objects to the Java.extend() function; see Extending Concrete Java Classes.
Comments
Post a Comment