Java Reference
In-Depth Information
Using a JavaScript Function
Sometimes a Java interface has only one method. In those cases, you can pass a JavaScript
function object in place of an implementation of the interface. The Runnable interface
in Java has only one method run() . When you need to use an instance of the Runnable
interface in JavaScript, you can pass a JavaScript function object. The following snippet
of code shows how to create a Thread object and start it. In the constructor of the Thread
class, a JavaScript function object myRunFunc is passed instead of an instance of the
Runnable interface:
function myRunFunc() {
print("A thread is running.");
}
// Call Thread(Runnable) constructor and pass the myRunFunc function
// object that will serve as an implementation for the run() method of
// the Runnable interface.
var thread = new java.lang.Thread(myRunFunc);
thread.start();
A thread is running.
Accessing Methods of a Superclass
In Java, when you can access the methods of a superclass using the keyword super .
When you extend a class in JavaScript, you can also access the methods of the superclass
using the Java.super() method. The method takes the JavaScript object that has been
extended in JavaScript and it returns a reference that can be used to call the method of the
superclass. Consider the code for a Person class as shown in Listing 6-4.
Listing 6-4. A Person Class
// Person.java
package com.jdojo.script;
public class Person {
private String firstName;
private String lastName;
public Person(String firstName, String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
 
Search WWH ::




Custom Search