Java Reference
In-Depth Information
}
/**
* @return the maxDepth
*/
public double getMaxDepth() {
return maxDepth;
}
/**
* @param maxDepth the maxDepth to set
*/
public void setMaxDepth(double maxDepth) {
this.maxDepth = maxDepth;
}
}
A method reference can now be used to assign the functionality of the calcu-
lateVolume() method to a variable. In the following code, the method reference is
assigned to a variable identified as volume , and then the volume variable is passed
to the calculateGallons() method.
PoolCalculator calculator = new PoolCalculator();
calculator.setLength(32);
calculator.setWidth(16);
calculator.setMinDepth(4);
calculator.setMaxDepth(8);
Volume volume = calculator::calculateVolume;
double poolVolume = volume.calculateVolume();
System.out.println("Volume of the pool is: " + poolVolume
+ " cubic feet");
System.out.println("Gallons in the pool: "
+ calculator.calculateGallons(volume));
Here is the result of running this code:
Volume of the pool is: 3072.0 cubic feet
Gallons in the pool: 22978.56
Search WWH ::




Custom Search