【问题标题】:Java object equals method not workJava对象等于方法不起作用
【发布时间】:2013-12-04 21:28:36
【问题描述】:

我的代码有问题,equals 方法不起作用,我尝试了== 和.equals(),但它不起作用。

这是main方法的代码:

x= new Card('♠',"number","3",5,Color.BLACK);
y= new Card('♠',"number","3",5,Color.BLACK);
if(x.equals(y)) System.out.println("True"); else System.out.println("False");

程序在屏幕上打印“False”。

这是我的卡片类:

package core;

import java.awt.Color;

public class Card {
    private char symbol;
    private String type,value;
    private int score;
    private Color warna;
    public Card(char symbol, String type, String value,int score,Color warna)
    {
        this.setSymbol(symbol);
        this.setValue(value);
        this.setType(type);
        this.setScore(score);
        this.warna = warna;
    }
    public char getSymbol() {
        return symbol;
    }
    public void setSymbol(char symbol) {
        this.symbol = symbol;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    public Color getWarna() {
        return warna;
    }
    public void setWarna(Color warna) {
        this.warna = warna;
    }
}

我该怎么办?

【问题讨论】:

  • 如果你的 equals 方法有问题,你应该在这里发布。
  • 你是否覆盖了Card中的equals方法?
  • == 只是比较引用并查看它们是否指向同一个对象。 'Equals()' 应该比较对象,但是如果你创建一个新类型,你必须指定比较是如何工作的。
  • 你没有在你的类中重写方法equals。谷歌“如何覆盖equals()”.
  • @Mukhlas1940 如果你想使用 equals() 来比较你的对象,你需要。

标签: java


【解决方案1】:

在你的情况下:

对:if ( x.equals(y) )

错误:if ( x == y )

如果equals API 不起作用,那么您在Card 类中覆盖了它,并且您实现了它错误。

如果你还没有覆盖它,那就去做吧:

public class Card 
{
    ...
    
    @Override
    public boolean equals(final Object obj)
    {
        if (obj == this)
            return true;
            
        if (obj == null || !(obj instanceof Card)) 
            return false;
        
        Card otherCard = (Card) obj;
        
        if (otherCard.score != this.score)       return false;
        if (otherCard.symbol != this.symbol)     return false;
        if (!otherCard.warna.equals(this.warna)) return false;
        if (!otherCard.type.equals(this.type))   return false;
        if (!otherCard.value.equals(this.value)) return false;
        
        return true;
    }

}

【讨论】:

    【解决方案2】:

    这不是使用equals() 方法,而是测试两个实例的引用相等性:

    if(x==y)
    

    如果要使用,需要调用equals()方法。

    更新:

    equals() 测试不起作用,因为您没有覆盖默认的 Object.equals() 方法,该方法仅测试引用相等性。

    【讨论】:

      【解决方案3】:

      默认情况下equals() 方法继承自超类。如果你的类没有扩展任何类,那么它是从 Object 类继承的,所以它看起来像

      public boolean equals(Object obj) {
          return (this == obj);
      }
      

      所以现在它只检查您是否将对象与自身进行比较。在这种情况下,您需要重写此方法以检查您是否将对象与同一类(或派生类)的对象进行比较,比较比较对象的字段值。以下是此类实现的示例。

      @Override
      public boolean equals(Object obj) {
          if (this == obj)
              return true;
          if (obj == null || !(obj instanceof YourClass))
              return false;
      
          YourClass other = (YourClass) obj;
          //if field1 is primitive type
          if (field1 != other.field1)
              return false;
          //if field2 is object type
          if (field2 == null || other.field2 != null)
              return false;
          else if (!field2.equals(other.field2))
              return false;
          //rest of fields
          return true;
      }
      

      【讨论】:

        【解决方案4】:

        因为“==”只检查 x 和 y 是否指向同一个对象。要检查它们是否相等,您需要覆盖 Card 类中 Object 类中的 Object.equals(Object) 方法,然后执行 if(x.equals(y)) 而不是 if(x == y)

        http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html

        这是一个有点复杂的过程,但基本上你会想要...

        @Override
        boolean equals(Object obj)
        {
           if(this == obj) //Check if they're the same object
           return true;
           else if(!(obj instanceof Card)) //Check if it is not a Card
           return false
           else //Else...
           Card other = (Card) obj;
           /*Check if the variables are equal*/
        }
        

        【讨论】:

          【解决方案5】:

          if(x==y) 仅当两个变量都引用同一个对象时才计算为 true。

          对于equals,你是否重写了equals方法?比较两个对象时最好重写equals方法。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-04-12
            • 2013-10-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-07-12
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多