Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 转换为泛型类_Java_Generics_Generic Programming - Fatal编程技术网

Java 转换为泛型类

Java 转换为泛型类,java,generics,generic-programming,Java,Generics,Generic Programming,我很难理解泛型的概念。我需要将类数据集转换为它的通用形式。我尤其不知道如何处理数据集的字段。我明白我们必须用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 dat

我很难理解泛型的概念。我需要将类数据集转换为它的通用形式。我尤其不知道如何处理数据集的字段。我明白我们必须用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;
   }
}
/**
计算一组数据值的平均值。
*/
公共类数据集
{
私人双和;
私人可测最大值;
私人整数计数;
/**
构造一个空数据集。
*/
公共数据集()
{
总和=0;
计数=0;
最大值=零;
}
/**
将数据值添加到数据集。
@参数x数据值
*/
公共空间添加(x)
{
sum=sum+x.getMeasure();
if(count==0 | | max.getMeasure()
更新: 以下是我提出的解决方案:

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;
   }
}
公共类DataSetGen
{
私人双和;
私人T最大值;
私人整数计数;
/**
构造一个空数据集。
*/
公共数据集()
{
总和=0;
计数=0;
最大值=零;
}
/**
将数据值添加到数据集。
@参数x数据值
*/
公共无效添加(T x)
{
sum=sum+x.getMeasure();
if(count==0 | | max.getMeasure()
首先问自己一个问题:“这应该是一组???”。在您的例子中,它可能是
可测量的
的一个子类的集合


现在您已经有了这些,您应该能够确定将泛型类型放在何处。

到目前为止您尝试了什么?通常泛型类的定义类似于
公共类数据集
,然后用
T
替换要添加的对象的所有实例,例如
public void add(T x)
。或您的私有变量
private T max
您可能需要
数据集
,然后将所有
可测量的
替换为
T
。Purpose作为
getMaximum()
的返回类型可以更好地键入。我刚刚更新了答案。我不确定我是否正确。我要把它交给我的教授。我希望它是正确的。非常感谢大家的帮助。这也是我关于stackoverflow的第一篇文章,因此,我为违反一些规则表示歉意。我不确定我是否明白了。我确实更新了我的答案。我想我只需要添加:公共类DataSetGen