为User实体类编写equals方法与hashCode方法

package com.Entity;

public class User {
	private int id;
	private String username;
	private String password;
	private int age;
	private String gender;

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!(obj instanceof User)) {
			return false;
		}
		final User user = (User) obj;
		return this.id == user.id && this.username.equals(user.username) && this.password.equals(user.password)
				&& this.age == user.age;
	}

	public int hashCode() {
		int result = 1;
		result = 31 * result + id;
		result = 31 * result + username.hashCode();
		result = 31 * result + password.hashCode();
		result = 31 * result + age;
		result = 31 * result + gender.hashCode();
		return result;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + ", age=" + age + ", gender="
				+ gender + "]";
	}

}

你可能感兴趣的:(为User实体类编写equals方法与hashCode方法)