如何比较Object类?

我试过了

if ("0.0".equals(price) ||"0.0".equals(quantity))



if: (price.equals("0.0") ||quantity.equals("0.0"))

最后,它总是打印0.0,但是if没有任何反应!

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){for (int i = model1.getRowCount() - 1; i >= 0; i--) {
        Object price = model1.getValueAt(i, tbl_order_detail.getColumnCount() - 8);
        Object quantity = model1.getValueAt(i, tbl_order_detail.getColumnCount() - 11);
        //if ("0.0".equals(price) ||"0.0".equals(quantity))
       if (price.equals("0.0") ||quantity.equals("0.0"))
        {
            model1.removeRow(i);
            System.out.println("price&: "+price);
            System.out.println("quantity&: "+quantity);
        }else{
            System.out.println("price: "+price);
            System.out.println("quantity: "+quantity);
        }
    }
       }

最佳答案

在Java中,在对象上调用equals使用对象的标识。除非对getQuantity和getPrice的调用返回字符串,否则它们将不相等。我建议您不要使用原始对象,而应为实例使用实际类型,如果需要执行将字符串与双精度进行比较之类的操作,请覆盖等于。

关于java - 如何与对象类进行比较?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41654393/

10-12 03:40
查看更多