1 要点
1 定义私有属性
2 定义setter和getter方法
3 定义有参和无参构造器
4 重写父类的equals和hashCode方法
5 添加序列化版本ID
2 equals方法分析
- 自反性,判断是不是传入的当前对象
- 判断传入的是否为null
- 判断传入对象是否是同一类型
- 判断各个属性字段是否相等(属性字段需要根据自己需要比较的实际需求进行比较)
@Override
public boolean equals(Object obj) {
// 1. 自反性 x.equals(x) == true
if (this == obj)
return true;
// 2. obj为空直接返回false
if (obj == null)
return false;
// 3. 判断是不是同一个类型
if (getClass() != obj.getClass())
return false;
/**
* 4. 比较各个属性部分
*/
JavaEntity other = (JavaEntity) obj; // 强转
// bool类型比较
if (boolean_ != other.boolean_)
return false;
// double类型比较,转化成long
if (Double.doubleToLongBits(double_) != Double
.doubleToLongBits(other.double_))
return false;
// float类型比较转化成int
if (Float.floatToIntBits(float_) != Float.floatToIntBits(other.float_))
return false;
// 数组类型比较,调用重载方法Arrays.equals
if (!Arrays.equals(intArr, other.intArr))
return false;
// int类型
if (int_ != other.int_)
return false;
// long类型
if (long_ != other.long_)
return false;
// 对象类型,调用equals对象必须非空,否则调用equals会报错
if (string == null) {
if (other.string != null)
return false;
} else if (!string.equals(other.string))
return false;
return true;
}
3 hashCode方法分析
- 确定一个相乘的基数prime(如31),确定一个初始值result(如1)
- 每个有意义的字段处理后得到的值迭代进去 ,迭代方法为:result = prime*result + 属性的int值
- 计算属性的int值的方法根据类型各不相同,详细见代码注释
@Override
public int hashCode() {
final int prime = 31; // 相乘的基数
int result = 1; // 初始值
// 1. 布尔类型true和false各对应一个整数
result = prime * result + (boolean_ ? 1231 : 1237);
// 2. double类型调用Double.doubleToLongBits转化成long类型,然后和他的高32位异或
long temp;
temp = Double.doubleToLongBits(double_);
result = prime * result + (int) (temp ^ (temp >>> 32));
// 3. float类型Float.floatToIntBits
result = prime * result + Float.floatToIntBits(float_);
// 4. 数组类型,调用Arrays.hashCode重载方法将每一位按照基本数据类型处理
result = prime * result + Arrays.hashCode(intArr);
// 5. int类型直接相加
result = prime * result + int_;
// 6. long类型和他的高32位异或
result = prime * result + (int) (long_ ^ (long_ >>> 32));
// 7. 对象类型,null返回0,非空调用自身的hashCode方法
result = prime * result + ((string == null) ? 0 : string.hashCode());
return result;
}
4 完整代码
public class JavaEntity implements Serializable{
// 版本号
private static final long serialVersionUID = 1L;
// 私有属性
private boolean boolean_;
private int int_;
private long long_;
private float float_;
private double double_;
private String string;
private int[] intArr;
// 构造方法
public JavaEntity() {
}
public JavaEntity(boolean boolean_, int int_, long long_, float float_,
double double_, String string, int[] intArr) {
super();
this.boolean_ = boolean_;
this.int_ = int_;
this.long_ = long_;
this.float_ = float_;
this.double_ = double_;
this.string = string;
this.intArr = intArr;
}
/**
* hashCode方法
*/
@Override
public int hashCode() {
final int prime = 31; // 相乘的基数
int result = 1; // 初始值
// 1. 布尔类型true和false各对应一个整数
result = prime * result + (boolean_ ? 1231 : 1237);
// 2. double类型调用Double.doubleToLongBits转化成long类型,然后和他的高32位异或
long temp;
temp = Double.doubleToLongBits(double_);
result = prime * result + (int) (temp ^ (temp >>> 32));
// 3. float类型Float.floatToIntBits
result = prime * result + Float.floatToIntBits(float_);
// 4. 数组类型,调用Arrays.hashCode重载方法将每一位按照基本数据类型处理
result = prime * result + Arrays.hashCode(intArr);
// 5. int类型直接相加
result = prime * result + int_;
// 6. long类型和他的高32位异或
result = prime * result + (int) (long_ ^ (long_ >>> 32));
// 7. 对象类型,null返回0,非空调用自身的hashCode方法
result = prime * result + ((string == null) ? 0 : string.hashCode());
return result;
}
/**
* equals方法
*/
@Override
public boolean equals(Object obj) {
// 1. 自反性 x.equals(x) == true
if (this == obj)
return true;
// 2. obj为空直接返回false
if (obj == null)
return false;
// 3. 判断是不是同一个类型
if (getClass() != obj.getClass())
return false;
/**
* 4. 比较各个属性部分
*/
JavaEntity other = (JavaEntity) obj; // 强转
// bool类型比较
if (boolean_ != other.boolean_)
return false;
// double类型比较,转化成long
if (Double.doubleToLongBits(double_) != Double
.doubleToLongBits(other.double_))
return false;
// float类型比较转化成int
if (Float.floatToIntBits(float_) != Float.floatToIntBits(other.float_))
return false;
// 数组类型比较,调用重载方法Arrays.equals
if (!Arrays.equals(intArr, other.intArr))
return false;
// int类型
if (int_ != other.int_)
return false;
// long类型
if (long_ != other.long_)
return false;
// 对象类型,调用equals对象必须非空,否则调用equals会报错
if (string == null) {
if (other.string != null)
return false;
} else if (!string.equals(other.string))
return false;
return true;
}
public boolean isBoolean_() {
return boolean_;
}
public void setBoolean_(boolean boolean_) {
this.boolean_ = boolean_;
}
public int getInt_() {
return int_;
}
public void setInt_(int int_) {
this.int_ = int_;
}
public long getLong_() {
return long_;
}
public void setLong_(long long_) {
this.long_ = long_;
}
public float getFloat_() {
return float_;
}
public void setFloat_(float float_) {
this.float_ = float_;
}
public double getDouble_() {
return double_;
}
public void setDouble_(double double_) {
this.double_ = double_;
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
public int[] getIntArr() {
return intArr;
}
public void setIntArr(int[] intArr) {
this.intArr = intArr;
}
}
3979

被折叠的 条评论
为什么被折叠?



