Java Reference
In-Depth Information
Notice that the calculator object in JavaScript contains the implementation of all
abstract methods of the Java Calculator interface. The following statement creates an
implementation of the Calculator interface:
// Load the calculator object
load("calculator.js");
// Get the Java interface type
var Calculator = Java.type("com.jdojo.script.Calculator");
// Create an instance of the com.jdojo.script.Calculator interface
// passing its constructor a calculator JavaScript object
var calc = new Calculator(calculator);
Now you can start using the calc object as if it were an implementation of the
Calculator interface, as shown:
// Use the instance of teh Calculator interface
var x = 15.0, y = 10.0;
var addResult = calc.add(x, y);
var subResult = calc.subtract(x, y);
var mulResult = calc.multiply(x, y);
var divResult = calc.divide(x, y);
printf("calc.add(%.2f, %.2f) = %.2f", x, y, addResult);
printf("calc.subtract(%.2f, %.2f) = %.2f", x, y, subResult);
printf("calc.multiply(%.2f, %.2f) = %.2f", x, y, mulResult);
printf("calc.divide(%.2f, %.2f) = %.2f", x, y, divResult);
calc.add(15.00, 10.00) = 25.00
calc.subtract(15.00, 10.00) = 5.00
calc.multiply(15.00, 10.00) = 150.00
calc.divide(15.00, 10.00) = 1.50
 
Search WWH ::




Custom Search