0

我是 Java 初学者。我想在下面的课程中实现一些基本功能。

/*Demonstrate the different data types in java*/
import java.io.*;
import java.util.*;

public class TypeDemo implements java.io.Serializable
{
    static byte b1=50;             /*byte variable     8 bits : (-128 TO 127) */
    static short s1=10;            /*short variable   16 bits : (-32,768 TO 32,767) */
    static int i1=-555;            /*int variable     32 bits : (-2,147,483,648 to 2,147,483,647) */
    static long l1=100000;         /*long variable    64 bits : (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807) */
    static float f1 = 123.2f;      /*float variable   32 bits : (4.9e-324 to 1.8e+308) (Approximate) */
    static double d1 = 1.4512;     /*double variable  64 bits : (1.4e-045 to 3.4e+038) (Approximate) */
    static char ch1 = 'Y';         /*char variable    16 bits : (0 - 65,536) (Needed for Unicode) */
    static boolean b = false;      /*boolean variable  1 bit  : (true OR false ) */

    public static void display()
    {
        System.out.println("The value of different variables");
        System.out.println("byte      : " + "b1  = " +b1);
        System.out.println("short     : " + "s1  = " +s1);
        System.out.println("int       : " + "i1  = " +i1);
        System.out.println("long      : " + "l1  = " +l1);
        System.out.println("float     : " + "f1  = " +f1);
        System.out.println("double    : " + "d1  = " +d1);
        System.out.println("char      : " + "ch1 = " +ch1);
        System.out.println("boolean   : " + "b   = " +b);
    }

    public boolean equals(Object aThat)
    {
        /*check for self comparision*/
        if (this == aThat) return true;

        /* use instanceof instead of getClass here for two reasons
         * 1. if need be, it can match any supertype, and not just one class;
         * 2. it renders an explict check for "that == null" redundant, since
         *    it does the check for null already - "null instanceof [type]" always
         * 3. returns false. (See Effective Java by Joshua Bloch.)
        */
        if ( !(aThat instanceof TypeDemo) ) return false;

        /* 
         * Alternative to the above line :
         * if ( aThat == null || aThat.getClass() != this.getClass() ) return false;
         */

           /*cast to native object is now safe*/
         TypeDemo that = (TypeDemo)aThat;

        /*
         *now a proper field-by-field evaluation can be made
         */

        return  EqualsUtil.areEqual(this.b1, that.b1) &&
                EqualsUtil.areEqual(this.s1, that.s1) &&
                EqualsUtil.areEqual(this.i1, that.i1) &&
                EqualsUtil.areEqual(this.l1, that.l1) &&
                EqualsUtil.areEqual(this.f1, that.f1) &&
                EqualsUtil.areEqual(this.d1, that.d1) &&
                EqualsUtil.areEqual(this.ch1,that.ch1) &&
                EqualsUtil.areEqual(this.b, that.b);
   }

   public static void main(String[] args)
   {
        display();
   }
}

我正在尝试覆盖equals()逐个字段比较对象的方法。我什至无法编译程序。

TypeDemo.java:55: error: cannot find symbol
    return  EqualsUtil.areEqual(this.b1, that.b1) &&
            ^
  symbol:   variable EqualsUtil
  location: class TypeDemo
TypeDemo.java:56: error: cannot find symbol
            EqualsUtil.areEqual(this.s1, that.s1) &&
            ^
  symbol:   variable EqualsUtil
  location: class TypeDemo
TypeDemo.java:57: error: cannot find symbol
            EqualsUtil.areEqual(this.i1, that.i1) &&
            ^
  symbol:   variable EqualsUtil
  location: class TypeDemo
TypeDemo.java:58: error: cannot find symbol
            EqualsUtil.areEqual(this.l1, that.l1) &&
            ^
  symbol:   variable EqualsUtil
  location: class TypeDemo
TypeDemo.java:59: error: cannot find symbol
            EqualsUtil.areEqual(this.f1, that.f1) &&

我已经导入java.util.*了它似乎无法找到EqualsUtil

4

2 回答 2

3

首先,EqualsUtil它不是 Java 库 (java.util.*) 的一部分
如果你看到这个

其次,当您需要覆盖equals方法时,您还必须覆盖hashcode
请参阅下面的示例

@Override
public boolean equals(Object obj) {
     if (obj == this){
        return true;
    }
    if (obj == null || obj.getClass() != this.getClass()){
        return false;
    }

    Person guest = (Person) obj;
    return id == guest.id
             &&(firstName == guest.firstName
                 || (firstName != null && firstName.equals(guest.getFirstName())))
             &&(lastName == guest.lastName
                 || (lastName != null && lastName .equals(guest.getLastName())));
}

@Override
public int hashCode(){
    final int prime = 31;
     int result = 1;
    result = prime * result
            + ((firstName == null) ? 0 : firstName.hashCode());
    result = prime * result + id;
     result = prime * result
            + ((lastName == null)? 0 : lastName.hashCode());
    return result;
 }

PS 示例归功于Javarevisited 网站

于 2013-07-03T04:19:33.640 回答
1

如果要比较两个对象是否相等,只需覆盖equals该对象的方法即可。并比较使用

  yourOneObject.equals(yourAnotherObject);

如果您要覆盖equals,则还要覆盖hashcode

你可以在这里找到一个非常好的教程。 http://www.thejavageek.com/2013/06/28/significance-of-equals-and-hashcode/

于 2013-07-03T04:21:25.620 回答