gpt4 book ai didi

java错误: cannot find symbol EqualsUtil. areEqual

转载 作者:行者123 更新时间:2023-12-01 18:46:47 26 4
gpt4 key购买 nike

我是 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 方法时,你还必须重写 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;
}

附注示例归功于Javarevisited site

关于java错误: cannot find symbol EqualsUtil. areEqual,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17439575/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com