equals方法是否与对象一起使用?如果是,如何? [英] Does the equals method work with objects? If so, how?

查看:178
本文介绍了equals方法是否与对象一起使用?如果是,如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序是动物园,在动物园里有分支的动物亚群,是爬行动物。当我做一个equals方法时,主程序编译并运行。我很困惑如果我比较对象,而不是特别是int或String,java知道如何使用equals方法?

  public class Zoo {

public static void main(String [] args){
Animal a = new Animal(Bob);
Reptile komodo = new Reptile(Snakey);
komodo.bask();
a.size = 3;
komodo.size = 5;
System.out.println(a);
System.out.println(komodo);
Turtle t = new Turtle(Slowy);
t.hide();
t.size = 6;
t.numlegs = 4;
System.out.println(t);
System.out.println(t.equals(komodo));
}
}

public class Animal {
public String name;

public boolean equals(Animal other){
return other.size == this.size;
}

public Animal(String s){
name = s;
}
public void setName(String n){
this.name = n;
}
public void eat(String meal){
System.out.println(chump chump yummy+ meal);
}
public int size;

public String toString(){
returnI am+ name +and I'm+ size +cm long;
}

}

public class Reptile extends Animal {

public Reptile(String n){
super );
numlegs = 0;
}
public Reptile(String n,int l){
super(n);
numlegs = l;
}

public void bask(){
System.out.println(basking ...);
}
public String toString(){
return super.toString()+ numlegs +legs;
}

public int numlegs;
}
public class Turtle extends Reptile {

public Turtle(String n){
super(n,4);
shellColor =Brown;
}
public void hide(){
System.out.println(you cant see me);
}
public String toString(){
return super.toString()+和我的shell是+ shellColor;
}
public String shellColor;

public void bask(){
super.bask();
System.out.println(turtle is basking ...);
}

}


解决方案<

您不会覆写 Object#equals 方法,但会超载。在您的方法声明中,您使用 Animal 类型代替对象

  public boolean equals(Animal other)

该方法的覆盖将使用 instanceof 运算符。显示示例:

  @Override 
public boolean equals(Object other){
if(other instanceof动物){
Animal otherAnimal =(Animal)other;
//比较逻辑...
}
return false;
}

有关主题的更多信息:




  • 关于equals的最佳做法:过载或不过载?

  • 覆盖Object.equals VS重载


    • I have a program that is zoo and in the zoo there are branched subgroups of animals that are reptiles. When I do an equals method the main program compiles and it runs. I'm confused how does java know to use the equals method if I'm comparing objects and not specifically int or String?

      public class Zoo {
      
          public static void main(String[]args) {
              Animal a=new Animal("Bob");
              Reptile komodo= new Reptile("Snakey");
              komodo.bask();
              a.size=3;
              komodo.size=5;
              System.out.println(a);
              System.out.println(komodo);
              Turtle t= new Turtle("Slowy");
              t.hide();
              t.size=6;
              t.numlegs=4;
              System.out.println(t);
              System.out.println(t.equals(komodo));
          }
      }
      
      public class Animal {
          public String name;
      
          public boolean equals(Animal other) {
              return other.size==this.size;
          }
      
          public Animal(String s) {
              name=s;
          }
          public void setName(String n) {
              this.name=n;
          }
          public void eat(String meal) {
              System.out.println("chump chump yummy "+meal);
          }
          public int size;
      
          public String toString() {
              return "I am "+name+" and I'm "+size+" cm long";
          }
      
      }
      
      public class Reptile extends Animal {
      
          public Reptile(String n) {
              super(n);
              numlegs=0;
          }
          public Reptile(String n, int l) {
              super(n);
              numlegs=l;
          }
      
          public void bask() {
              System.out.println("basking...");
          }
          public String toString() {
              return super.toString()+numlegs+" legs";
          }
      
          public int numlegs;
      }
      public class Turtle extends Reptile {
      
          public Turtle(String n) {
              super (n,4);
              shellColor="Brown";
          }
          public void hide() {
              System.out.println("you cant see me");
          }
          public String toString() {
              return super.toString()+" and my shell is"+ shellColor;
          }
          public String shellColor;
      
          public void bask() {
              super.bask();
              System.out.println("turtle is basking...");
          }
      
      }
      

      解决方案

      You're not overriding the Object#equals method, but overloading it. In your method declaration you use Animal type instead of Object:

      public boolean equals(Animal other)
      

      A good overriding of the method would be using the instanceof operator. Showing an example:

      @Override
      public boolean equals(Object other) {
          if(other instanceof Animal) {
              Animal otherAnimal = (Animal)other;
              //comparison logic...
          }
          return false;
      }
      

      More info on the subject:

      这篇关于equals方法是否与对象一起使用?如果是,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆