Java Reference
In-Depth Information
You can use the Java.extend() method to extends concrete classes, abstract classes,
and interfaces. The following code extends the concrete class java.lang.Thread and
implements the Calculator interface. The new implementation overrides the run()
method of the Thread class:
// Get the Calculator interface type
var Calculator = Java.type("com.jdojo.script.Calculator");
var Thread = Java.type("java.lang.Thread");
// Get a type that extends the Calculator type
var ThreadCalcExtender = Java.extend(Thread, Calculator);
// Implement the abstract methods in CalculatorExtender
// using an anonymous class like syntax
var calcThread = new ThreadCalcExtender() {
add: (function (n1, n2) n1 + n2),
subtract: (function (n1, n2) n1 - n2),
multiply: (function (n1, n2) n1 * n2),
divide: (function (n1, n2) n1 / n2),
run: function () {
var n1 = Math.random();
var n2 = Math.random();
printf("n1 = %.2f, n2 = %.2f", n1, n2);
var addResult = this.add(n1, n2);
printf("calc.add(%.2f, %.2f) = %.2f", n1, n2, addResult);
}
};
// Start the thread
calcThread.start();
n1 = 0.61, n2 = 0.66
calc.add(0.61, 0.66) = 1.27
Search WWH ::




Custom Search