我是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
。 最佳答案
首先,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;
}
附言归功于Javarevisited site的示例