转换为通用 class
Converting into Generic class
我无法理解泛型概念。我需要将 class DataSet 转换为其通用形式。我尤其不知道如何处理 DataSet 的字段。我明白我们必须用 T 替换所有签名。
/**
Computes the average of a set of data values.
*/
public class DataSet
{
private double sum;
private Measurable maximum;
private int count;
/**
Constructs an empty data set.
*/
public DataSet()
{
sum = 0;
count = 0;
maximum = null;
}
/**
Adds a data value to the data set.
@param x a data value
*/
public void add(Measurable x)
{
sum = sum + x.getMeasure();
if (count == 0 || maximum.getMeasure() < x.getMeasure())
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 Measurable getMaximum()
{
return maximum;
}
}
更新:
这是我想出的解决方案:
public class DataSetGen <T>
{
private double sum;
private T maximum;
private int count;
/**
Constructs an empty data set.
*/
public DataSetGen()
{
sum = 0;
count = 0;
maximum = null;
}
/**
Adds a data value to the data set.
@param x a data value
*/
public void add(T x)
{
sum = sum + x.getMeasure();
if (count == 0 || maximum.getMeasure() < x.getMeasure())
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 T getMaximum()
{
return maximum;
}
}
首先问自己这个问题 "This is supposed to be a set of ???"。在您的情况下,它可能是 Measurable
.
的一组子类
既然你已经有了这个,你应该能够弄清楚将泛型类型放在哪里。
我无法理解泛型概念。我需要将 class DataSet 转换为其通用形式。我尤其不知道如何处理 DataSet 的字段。我明白我们必须用 T 替换所有签名。
/**
Computes the average of a set of data values.
*/
public class DataSet
{
private double sum;
private Measurable maximum;
private int count;
/**
Constructs an empty data set.
*/
public DataSet()
{
sum = 0;
count = 0;
maximum = null;
}
/**
Adds a data value to the data set.
@param x a data value
*/
public void add(Measurable x)
{
sum = sum + x.getMeasure();
if (count == 0 || maximum.getMeasure() < x.getMeasure())
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 Measurable getMaximum()
{
return maximum;
}
}
更新: 这是我想出的解决方案:
public class DataSetGen <T>
{
private double sum;
private T maximum;
private int count;
/**
Constructs an empty data set.
*/
public DataSetGen()
{
sum = 0;
count = 0;
maximum = null;
}
/**
Adds a data value to the data set.
@param x a data value
*/
public void add(T x)
{
sum = sum + x.getMeasure();
if (count == 0 || maximum.getMeasure() < x.getMeasure())
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 T getMaximum()
{
return maximum;
}
}
首先问自己这个问题 "This is supposed to be a set of ???"。在您的情况下,它可能是 Measurable
.
既然你已经有了这个,你应该能够弄清楚将泛型类型放在哪里。