繁体   English   中英

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

[英]Java Generics - Multiple Questions (Type not within bounds, Warnings)

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

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

数据集

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;
}

可测量的

/**
   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);

}

矩形图形

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;
}

类型边界<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) {
        //...
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM