简体   繁体   English

Java:使用我自己的.equals将该类与该类中的另一个进行比较

[英]Java: Comparing a class with another within that class using a my own .equals

I am making a method .equals replacing the equals method used. 我正在制作一个方法.equals替换使用的equals方法。 It accepts a object. 它接受一个对象。 I want it to check if that object equals the class that runs the .equals class. 我希望它检查该对象是否等于运行.equals类的类。

I know I want to compare all the private methods I have to that object. 我知道我想比较我对该对象的所有私有方法。

Is there a way to do this without making another private class to get the private variables from the object? 有没有办法这样做而不需要另一个私有类来从对象获取私有变量? How do I do this to compare equality not identity? 我如何做这个比较平等而不是身份? I am stuck on this. 我坚持这个。 Do i have to use == to compare? 我必须使用==进行比较吗?

Also looking online i see others use recursion. 也在网上看我看到其他人使用递归。 If this is the way i have to do it can you show and explain it to me? 如果这是我必须这样做的方式,你可以向我展示和解释吗?

so an example i have 所以我有一个例子

public boolean equals(Object o)
{

this is in a class we will call bobtheBuilder (first thing to pop in my head) I want to check if the object o is equal to the class. 这是一个我们称之为bobtheBuilder的类(我头脑中要弹出的第一件事)我想检查对象o是否等于类。 bobTheBuilder has private object array and a private int. bobTheBuilder有私有对象数组和私有int。 I assume I want to check if the array and int of this class equal the array and int of the object. 我假设我想检查这个类的数组和int是否等于对象的数组和int。 First I know i need to check if it equals null so 首先我知道我需要检查它是否等于null

private _arrayObject[];

private _integerBob;

public boolean equals(Object o)
{
    boolean result = true;

    if (o == null)
        result = false

    bobTheBuilder compared = (bobTheBuilder) o;

    if(compared.getArray != _arrayObject[] || compared.getInt != _integerBob)
        result == false

    return result
}

I know that equal checks for equality not identity so I assume this is wrong. 我知道平等的检查不是身份,所以我认为这是错误的。 How would I do this 我该怎么做

So, you have a private array X and an int Y in a class Z. 因此,在类Z中有一个私有数组X和一个int Y.

Now, for checking the equality and not indentity, 现在,为了检查平等而不是缩进,

  1. First thing you should have setters and getters that will set and return the value of the private variables, eg 首先你应该有setter和getter来设置和返回私有变量的值,例如

public void setArrayX(Object[] arr), public Object[] getArrayX() public void setArrayX(Object [] arr),public Object [] getArrayX()

2.now, the eqauls method would look like, 2.现在,eqauls方法看起来像,

boolean equals(Object o) {
  if(Arrays.equals(this.getArrayX(),o.getArrayX()) && this.getIntY() == o.getIntY())
   return true;
  return false;
}

Adapted from the examples in Effective Java, 2nd edition by Joshua Bloch: 改编自Joshua Bloch的Effective Java第2版中的示例:

public boolean equals(Object o) {
  if (o == this) {
    return true;
  }

  if (!(o instanceof BobTheBuilder)) {
    return false:
  }

  BobTheBuilder bob = (BobTheBuilder) o; //guaranteed to succeed

  return Arrays.equals(bob._arrayObject, _arrayObject)
    && bob._integerBob == _integerBob;
}

Don't forget to override hashCode as well. 不要忘记重写hashCode

The text in the book refers to the contract set out for the equals method in Object which is here 书中的文字是指合同列明的equals的方法, Object在这里

I think the best way to check the class is through the 我认为检查课程的最佳方法是通过

someObject.getClass().isInstance(anotherObject)

command. 命令。

This works with inheritance as well. 这也适用于继承。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM