Java泛型 - 多个问题(类型不在边界内,警告)

时间:2015-06-17 23:02:38

标签: java generics

我有一些我需要通用的课程。我有以下类DataSet,我希望能够生成传递到实现我的可测量接口或我的Measurer接口的类的任何类型的对象的平均值和最大值。

当我去测试我的代码时,我得到的错误是我的类型不在范围内。有任何想法吗?当我从DataSet中排除Measurer时,测试编译。我不确定我是否正确地使用了DataSet。

DataSet.java

public class DataSet<T extends Measurable<Double> & Measurer<Double>>//
{
   /**
      Constructs an empty data set.
   */
   public DataSet()
   {
      this.sum = 0;
      this.count = 0;
      this.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;
   }

   private double sum;
   private T maximum;
   private int count;
}

DataSetTest.java

import java.awt.Rectangle;

public class DataSetTest
{
    public static void main(String[] args)
    {
        DataSet<BankAccount<Double>> ds1 = new DataSet<>();
        BankAccount<Double> ba1 = new BankAccount<>(100.00);
        BankAccount<Double> ba2 = new BankAccount<>(300.00);    
    }
}

BankAccount.java

public class BankAccount<T> implements Measurable<T>
{
    public BankAccount()
    {
        balance = null;
    }
    public BankAccount(T balance)
    {
        this.balance = balance;
    }
    public T getMeasure()
    {
        return balance;
    }
    private T balance;
}

Measurable.java

/**
   Describes any class whose objects can be measured.
*/
public interface Measurable <T>
{
   /**
      Computes the measure of the object.
      @return the measure
   */
   T getMeasure();
}

Measurer.java

/**
   Describes any class whose objects need a measurer.
*/
public interface Measurer <T>
{
   /**
      Computes the measurer of the object.
      @return the measurer
   */
    T getMeasurer(T anObject);

}

rectangleShape.java

import java.awt.Rectangle;

/**
Concrete Class RectangleMeasurer
@param Takes object as parameter. Overloads measurer class.
@return Returns the area of a object passed in.
*/
public class rectangleShape<T> implements Measurer<T>{
    public T getMeasurer(T anObject)
    {
        Rectangle aRectangle = (Rectangle) anObject;
        Double area = aRectangle.getWidth() * aRectangle.getHeight();

        //@SuppressWarnings("unchecked")
        a = (T)area;
        return a;
    }

    private T a;
}

1 个答案:

答案 0 :(得分:0)

类型边界<T extends Measurable<Double> & Measurer<Double>>意味着参数扩展了Measurable AND Measurer,但您希望它是OR。你不能用泛型参数语法来做到这一点。

但是,您可以在DataSet中重载方法。

public add(Measurable<Double> m) {
    //...
}

public add(Measurer<Double> m) {
    //...
}

或者您可以进行运行时类型检查,但这不是最合适的。

public add(Object o) {
    if (o instanceof Measurable) {
        //...
    } else if (o instanceof Measurer) {
        //...
    }
}

基本上,您的用例不需要类的类型参数。您试图使问题适合您的解决方案,而不是相反。