如何比较两个对象属性?

问题描述 投票:0回答:6
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
。希望我说清楚了。

java oop compare
6个回答
0
投票

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


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
方法


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

阅读本文以了解如何在 Object 类中重写 equals() ?

如果您严格想自己实现一个方法,请执行以下操作:

修改

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

更新

完成代码:

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

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);
        System.out.println(res);
    }
}

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

}

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

-1
投票
public boolean Compare(Object c1,Object c2) {
    return c1.brand == c2.brand && c1.price == c2.price;
}
© www.soinside.com 2019 - 2024. All rights reserved.