public class CellAtt {
private String brand;
private long serial;
private double price;
public CellAtt(String brand, long serial, double price) {
this.brand = brand;
this.serial = serial;
this.price = price;
}
public boolean Compare(Object c1,Object c2) {
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
CellAtt c1 = new CellAtt("nokia",4536895,3600.00);
CellAtt c2 = new CellAtt("Samsung",4536895,3600.00);
}
}
我在main.java
中创建了两个对象。
我想比较两个对象属性,Brand
和Price
。如果品牌和价格相同,则必须返回true
,否则false
。希望我清楚。
解决此问题的最佳方法是覆盖为此特定目的而构建的 Object.equals() 函数。更多细节可以在这里找到:https://www.tutorialspoint.com/java/lang/object_equals.htm
在Compare
方法中:
public boolean Compare(Object c1,Object c2){
c1 = (CellAtt)c1;
c2 = (CellAtt)c2;
}
方法return true
如果c1.brand.equals(c2.brand) && c1.price == c2.price
其他return false
。
或者,您可以重写Object
类equals()
方法
最好和好的方法就是重写类中的equals()
方法Object
:
public boolean equals(Object c){
if (c == null)
return false;
if (!CellAtt.class.isAssignableFrom(c.getClass()))
return false;
CellAtt obj = (CellAtt) c;
return this.brand.equals(obj.brand) && this.price == obj.price;
}
阅读本文以了解如何在 Object 类中覆盖 equals()?
如果您严格想自己实现一种方法,请执行以下操作:
修改类CellAtt
中的compare()
。
public boolean compare(CellAtt c){
if (c == null)
return false;
return this.brand.equals(c.brand) && this.price == c.price;
}
然后在Main
类中,您可以调用如下所示的方法:
boolean res = c1.compare(c2);
System.out.println(res);//this is added to check output
更新
Comlete code:
CellAtt
类:
public class CellAtt {
private String brand;
private long serial;
private double price;
public CellAtt(String brand, long serial, double price) {
this.brand = brand;
this.serial = serial;
this.price = price;
}
@Override
public boolean equals(Object c){
if (c == null)
return false;
if (!CellAtt.class.isAssignableFrom(c.getClass()))
return false;
CellAtt obj = (CellAtt) c;
return this.brand.equals(obj.brand) && this.price == obj.price;
}
//public boolean compare(CellAtt c){
// if (c == null)
// return false;
//
// return this.brand.equals(c.brand) && this.price == c.price;
//}
}
Main
类:
public class Main {
public static void main(String[] args) {
CellAtt c1 = new CellAtt("nokia",4536895,3600.00);
CellAtt c2 = new CellAtt("samsung",4536895,3600.00);
boolean res = c1.equals(c2);
// boolean res = c1.compare(c2);
System.out.println(res);
}
}
public class CellAtt {
private String brand;
private long serial;
private double price;
public CellAtt(String brand, long serial, double price) {
this.brand = brand;
this.serial = serial;
this.price = price;
}
@Override
public int hashCode() {
int hash = 42;
hash = 29 * hash + Objects.hashCode(this.brand);
hash = 29 * hash + (int) (this.serial ^ (this.serial >>> 32));
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final CellAtt other = (CellAtt) obj;
if (this.serial != other.serial) {
return false;
}
if (!Objects.equals(this.brand, other.brand)) {
return false;
}
return true;
}
}
public boolean Compare(Object c1, Object c2) {
if(c1 == null || c2 == null)
return false;
CellAtt ca1=(CellAtt)c1;
CellAtt ca2=(CellAtt)c2;
if(ca1.brand.equals(ca2.brand) && ca1.price == ca2.price)
return true;
return false;
}
public boolean Compare(Object c1,Object c2) {
return c1.brand == c2.brand && c1.price == c2.price;
}