如何比较两个对象属性?

时间:2017-07-15 04:17:32

标签: java oop

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中创建了两个对象 我想比较两个对象属性,即品牌和价格。如果品牌和价格相同,它必须返回true或者否则为false。希望我很清楚。  请帮忙

6 个答案:

答案 0 :(得分:1)

解决此问题的最佳方法是覆盖为此特定目的而构建的Object.equals()函数。更多细节可以在这里找到:https://www.tutorialspoint.com/java/lang/object_equals.htm

答案 1 :(得分:0)

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

或者,您可以覆盖equals()

Object方法

答案 2 :(得分:0)

执行此操作的最佳方式是覆盖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;
}

请阅读此内容以了解how to override equals() in Object class?

如果您严格要自己实施方法,请执行以下操作:

修改compare()课程中的CellAtt

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);
    }
}

答案 3 :(得分:-1)

public boolean Compare(Object c1,Object c2) {
    return c1.brand == c2.brand && c1.price == c2.price;
}

答案 4 :(得分:-1)

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;
}

答案 5 :(得分:-1)

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;
    }

}