Java Reference
In-Depth Information
is implemented within a JavaScript file. The example demonstrates custom method im-
plementation, as well as use of a default method. The following code is the interface,
PositionType , which will be implemented in JavaScript.
import java.math.BigDecimal;
public interface PositionType {
public double hourlyWage(BigDecimal hours, BigDecimal
wage);
/**
* Hourly salary calculation
* @param wage
* @return
*/
public default BigDecimal yearlySalary(BigDecimal
wage){
return (wage.multiply(new
BigDecimal(40))).multiply(new BigDecimal(52));
}
}
Next, let's take a look at the code within the JavaScript file that implements the
PositionType interface.
var somePosition = new
org.java8recipes.chapter18.recipe18_08.PositionType({
hourlyWage: function(hours, wage){
return hours * wage;
}
});
print(somePosition instanceof
Java.type("org.java8recipes.chapter18.recipe18_08.PositionType"));
var bigDecimal = Java.type("java.math.BigDecimal");
print(somePosition.hourlyWage(new bigDecimal(40), new
bigDecimal(12.75)));
Search WWH ::




Custom Search