关于接口的那段代码看不懂 谁帮我注释一下 这是老师给我们的程序
import java.awt.Rectangle;
/**
Computes the average of a set of data values.
*/
class DataSet
{
/**
Constructs an empty data set with a given measurer.
@param aMeasurer the measurer that is used to measure data values
*/
public DataSet(Measurer aMeasurer)
{
sum = 0;
count = 0;
maximum = null;
measurer = aMeasurer;
}
/**
Adds a data value to the data set.
@param x a data value
*/
public void add(Object x)
{
sum = sum + measurer.measure(x);
if (count == 0
|| measurer.measure(maximum) < measurer.measure(x))
maximum = x;
count++;
}
/**
Gets the average of the added data.
@return the average or 0 if no data has been added
*/
public double getAverage()
{
if (count == 0) return 0;
else return sum / count;
}
/**
Gets the largest of the added data.
@return the maximum or 0 if no data has been added
*/
public Object getMaximum()
{
return maximum;
}
private double sum;
private Object maximum;
private int count;
private Measurer measurer;
}
/**
This program demonstrates the use of a Measurer.
*/
public class DataSetTest
{
public static void main(String[] args)
{
Measurer m = new RectangleMeasurer();
DataSet data = new DataSet(m);
data.add(new Rectangle(5, 10, 20, 30));
data.add(new Rectangle(10, 20, 30, 40));
data.add(new Rectangle(20, 30, 5, 10));
System.out.println("Average area = " + data.getAverage());
Rectangle max = (Rectangle) data.getMaximum();
System.out.println("Maximum area rectangle = " + max);
}
}
/**
Describes any class whose objects can measure other objects.
*/
interface Measurer
{
/**
Computes the measure of an object.
@param anObject the object to be measured
@return the measure
*/
double measure(Object anObject);
}
/**
Objects of this class measure rectangles by area.
*/
class RectangleMeasurer implements Measurer
{
public double measure(Object anObject)
{
Rectangle aRectangle = (Rectangle) anObject;
double area = aRectangle.getWidth() * aRectangle.getHeight();
return area;
}
}
import java.awt.Rectangle;
/**
Computes the average of a set of data values.
*/
class DataSet
{
/**
Constructs an empty data set with a given measurer.
@param aMeasurer the measurer that is used to measure data values
*/
public DataSet(Measurer aMeasurer)
{
sum = 0;
count = 0;
maximum = null;
measurer = aMeasurer;
}
/**
Adds a data value to the data set.
@param x a data value
*/
public void add(Object x)
{
sum = sum + measurer.measure(x);
if (count == 0
|| measurer.measure(maximum) < measurer.measure(x))
maximum = x;
count++;
}
/**
Gets the average of the added data.
@return the average or 0 if no data has been added
*/
public double getAverage()
{
if (count == 0) return 0;
else return sum / count;
}
/**
Gets the largest of the added data.
@return the maximum or 0 if no data has been added
*/
public Object getMaximum()
{
return maximum;
}
private double sum;
private Object maximum;
private int count;
private Measurer measurer;
}
/**
This program demonstrates the use of a Measurer.
*/
public class DataSetTest
{
public static void main(String[] args)
{
Measurer m = new RectangleMeasurer();
DataSet data = new DataSet(m);
data.add(new Rectangle(5, 10, 20, 30));
data.add(new Rectangle(10, 20, 30, 40));
data.add(new Rectangle(20, 30, 5, 10));
System.out.println("Average area = " + data.getAverage());
Rectangle max = (Rectangle) data.getMaximum();
System.out.println("Maximum area rectangle = " + max);
}
}
/**
Describes any class whose objects can measure other objects.
*/
interface Measurer
{
/**
Computes the measure of an object.
@param anObject the object to be measured
@return the measure
*/
double measure(Object anObject);
}
/**
Objects of this class measure rectangles by area.
*/
class RectangleMeasurer implements Measurer
{
public double measure(Object anObject)
{
Rectangle aRectangle = (Rectangle) anObject;
double area = aRectangle.getWidth() * aRectangle.getHeight();
return area;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货