简体   繁体   English

实现.equals(Die aDie)方法和静态变量

[英]Implementing .equals(Die aDie) method and static variables

I am creating the method .equals(Die aDie) in my program. 我正在我的程序中创建方法.equals(Die aDie)。 Do I compare every instance variable including static ones? 我是否比较每个实例变量,包括静态变量?

boolean equals(Die aDie) 布尔等于(Die aDie)

is wrong, classes will call the equals(Object) method and ignore your equals(Die). 错了,类会调用equals(Object)方法并忽略你的equals(Die)。 Also implement the int hashCode() method using the same fields that equals(Object) uses. 还使用equals(Object)使用的相同字段实现int hashCode()方法。

 @Override public boolean equals(Object aDie){
     if(aDie == null || aDie.getClass() != Die.class)return false;
     if(aDie == this)return true;
     Die other = (Die)aDie;
     ...   
 }
 @Override public int hashCode(){
     ...
 }

You can ignore static fields since they are the same for every Die. 您可以忽略静态字段,因为它们对于每个Die都是相同的。

根据定义,静态变量不是实例变量,因此在同一个类的所有实例中始终相等。

Definitely not the static ones. 绝对不是静态的。

Whether you compare all the instance variables depends on what determines the "identity" of your objects, ie when do you consider them equal? 是否比较所有实例变量取决于决定对象“身份”的因素,即您何时认为它们相等? This can only be decided in the context of your particular application - we'd need more information. 这只能在您的特定应用程序的上下文中决定 - 我们需要更多信息。

For example, if you had a class representing books, you might only compare the ISBN number to determine whether two books are the same book, if you just wanted to store metadata about them (title, author). 例如,如果您有一个代表书籍的类,您可能只比较ISBN号以确定两本书是否是同一本书,如果您只想存储有关它们的元数据(标题,作者)。 If you merged two such databases, you'd want to eliminate duplicate records. 如果合并了两个这样的数据库,则需要消除重复记录。

BUT, if you were implementing a library catalogue of actual physical books, each individual copy is important and different, so you might compare ISBN and copy number. 但是,如果您实施的是实际实体图书的图书馆目录,则每个单独的副本都很重要且不同,因此您可以比较ISBN 副本编号。 If you merged two libraries, you'd expect to be able to detect duplicate copies. 如果合并了两个库,则可以检测到重复的副本。

When a variable is declared with the keyword “static”, its called a “class variable”. 当使用关键字“static”声明变量时,它称为“类变量”。 All instances share the same copy of the variable (always be equal across all instances of the same class). 所有实例共享变量的相同副本(在同一个类的所有实例中始终相等)。 A class variable can be accessed directly with the class, without the need to create a instance. 可以使用类直接访问类变量,而无需创建实例。

You would compare every instance variable. 您将比较每个实例变量。

Static variables are guaranteed to be equal, as they are class-specific, not instance-specific, so you don't have to worry about comparing them. 静态变量保证相同,因为它们是特定于类的,而不是特定于实例的,因此您不必担心比较它们。

这没有任何意义,因为你使用equals()比较实例因为它们是静态的:你会将它们与自己进行比较?

It is useless to compare static data, as it is shared among all instances of your Die class. 比较静态数据是没用的,因为它在您的Die类的所有实例之间共享。 But you can compare the various fields by accessing them directly (see example below). 但您可以通过直接访问它们来比较各个字段(请参阅下面的示例)。 Note that if your Die object has complex fields (such as instances of Map , Set , etc), you should also call the equals methods on those objects (again, see example below). 请注意,如果您的Die对象具有复杂字段(例如MapSet等实例),您还应该在这些对象上调用equals方法(再次参见下面的示例)。

If you want to provide an equals () method, you should override the one provided in the Object class ( equals (Object anOtherObject ), rather than overloading it, or at least make sure you also override equals (Object anOtherObject) to make sure that it returns the correct value too (the default implementation only checks if it's the same instance). And in your method you should then check if anOtherObject is an instance of Die . 如果你想提供一个equals ()方法,你应该覆盖Object类中提供的那个( equals (Object anOtherObject ),而不是重载它,或者至少确保你也重写equals (Object anOtherObject)以确保它也返回正确的值(默认实现只检查它是否是同一个实例)。然后在你的方法中,你应该检查anOtherObject是否是Die一个实例。

Here's an example, assuming that your Die class has 3 fields: String name , int value and Map<Integer> complexField : 这是一个例子,假设您的Die类有3个字段: String nameint valueMap<Integer> complexField

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

    if (!anOtherObject instanceof Die) {
        return false;
    }

    Die otherDie = (Die) anOtherObject;

    if (this.value != otherDie.value ||
            !this.name.equals (otherDie.name) ||
            !this.complexField.equals (otherDie.complexField)) {
        return false;
    }

    return true;
}

Josh Bloch's " Effective Java " has a very details section on how to correctly implement equals . Josh Bloch的“ Effective Java ”有一个关于如何正确实现equals的非常详细的部分。 You should definitely read it. 你一定要读它。

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

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