2013-07-03 170 views
0

我是一名初學者,我正在試圖在下面的類中實現一些基本的功能。java錯誤:無法找到符號EqualsUtil.areEqual

/*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()方法,通過field.I物場比較我甚至無法編譯程序。

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

+1

java.util'包中沒有'EqualsUtil'類。您需要檢查「EqualsUtil」的文檔以獲取必要的導入/引用。附:請僅發佈相關代碼。 –

回答

3

首先,EqualsUtil不是Java庫的一部分(的java.util。*)
如果你看到this

其次,當你需要重寫equals方法你也有覆蓋的哈希碼
請參閱以下示例

@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 site