【问题标题】:Overriding equals method to accept doubles inside of Object class. Java重写 equals 方法以在 Object 类中接受双精度。爪哇
【发布时间】:2015-02-20 20:50:44
【问题描述】:

在 equals 方法中比较双精度时,我似乎遇到了问题。我收到一条错误消息,提示 java.lang.ClassCastException: and java.lang.Double 无法转换为项目。我没有正确投射吗?当我将演员更改为 Double 时,我收到错误 cannot find symbol - method getPrice().

public class Item implements Comparable
{
    private String name, category;
    private int quantity;
    private double price;

    public Item(String nam,String cate,int quant,double pric)
    {
        name = nam;
        category = cate;
        quantity = quant;
        price = pric;
    }

    public String toString()
    {
        return name + ", " + category + ", " + quantity + ", " + price;
    }

    public boolean equals(Object other)
    {
        if (price <= ((Item)other).getPrice()) // Error //
        return true;
        else
        return false;
    }

    public String getName()
    {
        return name;
    }

    public String getCategory()
    {
        return category;
    }

    public int getQuantity()
    {
        return quantity;
    }

    public double getPrice()
    {
        return  price;
    }

    public int compareTo(Object other)
    {
        int result;

        double otherPrice = ((Item)other).getPrice();
        String otherCategory = ((Item)other).getCategory();

        if (this.equals(otherPrice))
            result = category.compareTo(otherCategory);
        else
            result = price > otherPrice ? + 1 : price < otherPrice ? -1 : 0;
        return result;
    }
}

【问题讨论】:

标签: java object boolean double


【解决方案1】:

我假设传入 compareTo 方法的 other 是一个 Item。

所以你有一个 Item other 和一个 double otherPrice。

然后,当您在 if 语句中调用 this.equals(otherPrice) 时,您正在执行 Item.equals(Double)。

您应该传入一个项目。我想你想用ObjectotherPrice = ((Item)other);替换double otherPrice = ((Item)other).getPrice();

看看你是否将一个双精度值传递给你尝试将双精度值转换为一个项目的 equals 方法,女巫是不正确的。

【讨论】:

    【解决方案2】:

    您的问题在于您的 compareTo 方法。您正在尝试将您的 Item Object 与 double 进行比较。 您的方法应如下所示:

    public int compareTo(Object other) {
        int result;
    
        double otherPrice = ((Item) other).getPrice();
        String otherCategory = ((Item) other).getCategory();
    
        if (this.price==otherPrice)
            result = category.compareTo(otherCategory);
        else
            result = price > otherPrice ? +1 : price < otherPrice ? -1 : 0;
        return result;
    }
    

    【讨论】:

      【解决方案3】:

      请注意,Java 中的 equals 方法将 Object 作为参数。 compareTo 方法也是如此。这意味着参数可以是 any 类型(String、List、Double...)。我怀疑这就是这里发生的事情。

      在equals 方法中,将参数other 转换为Item。但是如果other 不是Item 呢?

      您应该在使用instanceof 运算符进行转换之前检查other 的类型,如下所示:

      public boolean equals(Object other) {
          if (!(other instanceof Item)) { //this also includes the case where other is null
              return false;
          }
          return price <= ((Item)other).getPrice();
      }
      

      最好的做法是在this 上添加一个检查,以便完全避免演员:

      public boolean equals(Object other) {
          if (this == other) {
              return true;
          }
          if (!(other instanceof Item)) {
              return false;
          }
          return price <= ((Item)other).getPrice();
      }
      

      考虑到这一点,您还应该检查您的 compareTo 方法。

      【讨论】:

      • 我喜欢这个建议,非常正确。我认为这里真正的问题是他将 double 传递给 equals 方法,然后将其转换为 Item。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-19
      • 1970-01-01
      • 2020-08-08
      • 2015-01-26
      • 1970-01-01
      • 2015-05-23
      相关资源
      最近更新 更多