【问题标题】:How to compare two object properties?如何比较两个对象属性?
【发布时间】:2017-07-15 04:17:32
【问题描述】:
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 中创建了两个对象。

我想比较两个对象属性,BrandPrice。如果品牌和价格相同,则必须返回true,否则返回false。希望我很清楚。

【问题讨论】:

  • 比较品牌和价格是什么意思?
  • 这是我的作业,问题是这样的。“设计应该允许将一部手机与另一部手机进行比较以获得相等。如果两个手机对象被认为是相等的同品牌同价。”
  • 所以你的任务包括比较"nokia" == 3600.00?
  • 好的好的,我现在明白了。你在问类似obj1.price == obj2.priceobj1.brand.equals(obj2.brand) 的问题。由于您的属性是private,因此您已经实现了一个方法,但您不知道如何实现该方法。是这个问题吗?
  • 不,如果对象 1 的品牌等于对象 2 的品牌,则它们是两个对象,则为 true,价格遵循相同的程序

标签: java oop compare


【解决方案1】:

解决这个问题的最好方法是重写 Object.equals() 函数,该函数是为这个非常特殊的目的而构建的。更多细节可以在这里找到:https://www.tutorialspoint.com/java/lang/object_equals.htm

【讨论】:

    【解决方案2】:

    在您的Compare 方法中:

    public boolean Compare(Object c1,Object c2){
       c1 = (CellAtt)c1;
       c2 = (CellAtt)c2; 
    } 
    

    方法return true if c1.brand.equals(c2.brand) && c1.price == c2.price else return false

    或者,您可以覆盖Object 类的equals() 方法

    【讨论】:

    • 不要用“==”比较字符串。 Object c1 没有字段 brandprice
    • 是的,我弄错了,谢谢。
    【解决方案3】:

    最好的方法是覆盖Object 类中的equals() 方法:

    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?

    如果你完全想自己实现一个方法,这样做:

    修改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
    

    更新

    完整代码:

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

    【讨论】:

    • 我收到此错误“未定义 CellAtt 类型的方法 compare(CellAtt)
    • 检查参数数据类型,否则可能是拼写错误。复制并粘贴它,看看它是否有效。
    • @SreemanJuvvadi 很高兴为您提供帮助。所以你可以接受这个答案。它将帮助谁找到此类问题的答案。
    • c1.equals("abc") 抛出 ClassCastException。
    【解决方案4】:
    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;
        }
    
    }
    

    【讨论】:

      【解决方案5】:
      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;
      }
      

      【讨论】:

      • 如何在Main.java中调用Campare方法?
      • 新 CellAtt().Compare(c1,c2);
      【解决方案6】:
      public boolean Compare(Object c1,Object c2) {
          return c1.brand == c2.brand && c1.price == c2.price;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-31
        • 2023-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-10
        • 2021-05-08
        相关资源
        最近更新 更多