Java Reference
In-Depth Information
}
private double sum;
private Coin maximum;
private int count;
}
Clearly, the fundamental mechanics of analyzing the data is the same in all cases, but
the details of measurement differ.
Suppose that the various classes agree on a single method getMeasure that obtains
the measure to be used in the data analysis. For bank accounts, getMeasure returns
the balance. For coins, getMeasure returns the coin value, and so on. Then we can
implement a single reusable DataSet class whose add method looks like this:
sum = sum + x .getMeasure() ;
if (count == 0 || maximum .getMeasure() <
x .getMeasure() )
maximum = x;
count++;
What is the type of the variable x ? Ideally, x should refer to any class that has a
getMeasure method.
A Java interface type declares a set of methods and their signatures.
In Java, an interface type is used to specify required operations. We will define an
interface type that we call Measurable :
public interface Measurable
{
double getMeasure();
}
389
390
The interface declaration lists all methods that the interface type requires. The
Measurable interface type requires a single method, but in general, an interface
type can require multiple methods.
Note that the Measurable type is not a type in the standard libraryȌit is a type that
was created specifically for this topic, in order to make the DataSet class more
reusable.
Search WWH ::




Custom Search