【发布时间】:2021-03-17 23:31:30
【问题描述】:
我正在尝试在 java 中创建一个方法来比较一些产品。我需要按计量单位然后按数量比较它们,但我不知道如何。我需要使用 object.equals 吗? 我会把我的代码放在这里。
public abstract class Produs implements Comparable {
private String tipProdus;
private String unitateMasura;
private int cantitate;
public Produs(String tipProdus, String unitateMasura, int cantitate) {
super();
this.tipProdus = tipProdus;
this.unitateMasura = unitateMasura;
this.cantitate = cantitate;
}
public Object genereazaDescriere() {
Object String = null;
return String;
}
public void compareTo() {
tipProdus.equals(unitateMasura);{
}
}
}
【问题讨论】:
-
这能回答你的问题吗? How to compare objects by multiple fields
-
我不这么认为,我有一些产品,所以它们是字符串,我认为我需要使用.equals
-
你想通过检查
tipProdus.equals(unitateMasura)来在我们的代码中实现什么??你看过Comparablejavadoc - 你的代码根本没有实现类似的接口。 This link 给出了一些正确实现的例子。此外,您似乎需要了解如何比较具有不同unitateMasura的对象,很可能应该进行一些转换(例如英寸到毫米等)
标签: java class object interface comparable