如何写一个标准的实体类

1 要点

1 定义私有属性
2 定义setter和getter方法
3 定义有参和无参构造器
4 重写父类的equals和hashCode方法
5 添加序列化版本ID

2 equals方法分析

  1. 自反性,判断是不是传入的当前对象
  2. 判断传入的是否为null
  3. 判断传入对象是否是同一类型
  4. 判断各个属性字段是否相等(属性字段需要根据自己需要比较的实际需求进行比较)
@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方法分析

  1. 确定一个相乘的基数prime(如31),确定一个初始值result(如1)
  2. 每个有意义的字段处理后得到的值迭代进去 ,迭代方法为:result = prime*result + 属性的int值
  3. 计算属性的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;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值