Java 如何比较两个对象属性?
我在Java 如何比较两个对象属性?,java,oop,compare,Java,Oop,Compare,我在main.java中创建了两个对象 我想比较两个对象属性,品牌和价格。如果品牌和价格相同,则必须返回true,否则返回false。希望我很清楚。在您的比较方法中: public class CellAtt { private String brand; private long serial; private double price; public CellAtt(String brand, long serial, double price) {
main.java
中创建了两个对象
我想比较两个对象属性,
品牌
和价格
。如果品牌和价格相同,则必须返回true
,否则返回false
。希望我很清楚。在您的比较方法中:
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);
}
}
方法返回true
ifc1.brand.equals(c2.brand)和&c1.price==c2.price
else返回false
或者,您可以重写Object
class的equals()
方法。解决此问题的最佳方法是重写为此特定目的而构建的Object.equals()函数。可以在此处找到更多详细信息:最好的方法是在类中重写该方法:
读这篇文章来理解
如果您严格希望自己实现某个方法,请执行以下操作:
在CellAtt
类中修改compare()
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;
}
然后在Main
类中,可以调用如下方法:
public boolean compare(CellAtt c){
if (c == null)
return false;
return this.brand.equals(c.brand) && this.price == c.price;
}
更新
删除代码:
CellAtt
class:
boolean res = c1.compare(c2);
System.out.println(res);//this is added to check output
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
class:
boolean res = c1.compare(c2);
System.out.println(res);//this is added to check output
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;
//}
}
不要将字符串与“==”进行比较。 <代码>对象c1
没有字段品牌
或价格
。是的,我弄错了,谢谢。比较品牌和价格是什么意思?这是我的作业,问题是这样的。。“设计应允许将一部手机与另一部手机进行同等的比较。如果两部手机具有相同的品牌和相同的价格,则认为它们是同等的。”因此,您的任务包括比较“nokia”==3600.00
?好的,我现在知道了。你问的是这样的问题obj1.price==obj2.price
和obj1.brand.equals(obj2.brand)
。因为您的属性是私有的,所以您已经实现了一个方法,并且不知道如何实现该方法。这就是问题所在吗?不,它们是两个对象,如果对象1的品牌等于对象2的品牌,则为true,并遵循与price相同的过程如何在Main.java?new CellAtt()中调用Campare方法。比较(c1,c2);我收到这个错误“CellAtt类型的方法比较(CellAtt)未定义检查参数数据类型,或者可能是拼写错误。复制并粘贴这个,看看它是否有效。@SreemanJuvvadi很乐意提供帮助。所以你可以接受这个答案。它将帮助你找到这样问题的答案。c1.equals(“abc”)
抛出ClassCastException。
boolean res = c1.compare(c2);
System.out.println(res);//this is added to check output
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;
//}
}
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);
}
}