`
jxqc_job
  • 浏览: 529 次
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
重写hashCode和equals方法2 重写hashcode和equals方法2
1. Person.java
package com.pojo;

public class Person {
	private Integer id;
	private String sex; //
	private Integer age;
	private String name;

	@Override
	public int hashCode() {
		Integer h = 0;
		int s = h.hashCode();
		if(id != null){
			s += id.hashCode(); 
		}
		if(name != null){
			s += name.hashCode();
		}
		if(sex != null){
			s += sex.hashCode();
		}
		if(age != null){
			s += age.hashCode();
		}
		return s;
	}
	/**那本类的对象与参数Obj进行是否相等比较*/
	@Override
	public boolean equals(Object obj) {
		if(this == obj){
			return true;
		}
		return false;
	}
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

2. Student.java
package com.pojo;

public class Student extends Person{
	private String country; //国籍
	public Student(){
		super();
	}
	@Override
	public int hashCode() {
		int h = super.hashCode();
		if(this.country != null){
			h += this.country.hashCode();
		}
		return h;
	}
	/**将本类的对象与参数Obj进行是否相等比较*/
	@Override
	public boolean equals(Object obj) {
		Student s = null;
		if(obj instanceof Student){
			s = (Student) obj;
		}
		if((s.getId() != null && s.getId() == s.getId())
				&& (s.getName() != null && super.getName().equals(s.getName()))
				&& (s.getAge() != null && super.getAge() == s.getAge())
				&& (s.getSex() != null && super.getSex().equals(s.getSex()))
				&& (s.country != null && s.country.equals(this.country))){
			return true;
		}
		return false;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}
	
}

3. CopyDemo2.java
package com.test;

import com.pojo.Student;
/**
 * 测试类--模拟批量复制
 */
public class CopyDemo2 {
	public static void main(String[] args) {	
		Student s = new Student();
		s.setId(2013001);
		s.setAge(12);
		s.setCountry("中国");
		s.setName("李四");
		s.setSex("男");
		
		Student s2 = new Student();
		s2.setId(2013001);
		s2.setAge(12);
		s2.setCountry("中国");
		s2.setName("李四");
		s2.setSex("男");
		
		Student s3 = new Student();
		s3.setId(2013001);
		s3.setAge(12);
		s3.setCountry("中国");
		s3.setName("李四");
		s3.setSex("女");
		
		Student s4 = new Student();
		System.out.println("s == s2 ? "+(s.equals(s2)?"相等":"不相等"));
		System.out.println("s == s3 ? "+(s.equals(s3)?"相等":"不相等"));
		System.out.println("s == s4 ? "+(s.equals(s4)?"相等":"不相等"));
		//对象的copy
		s4 = s;
		System.out.println("从s复制后s == s4 ? "+(s.equals(s4)?"相等":"不相等"));
		int i = 2; 
		Student[] sd = null;
		if(i > 0){
			sd = new Student[i];
		}
		for(int j = 0; j < i; j++){
			sd[j] = s;
		}
		for(int j = 0; j< sd.length; j++){
			System.out.println("从s复制后s == sd["+j+"] ? "+(s.equals(sd[j])?"相等":"不相等"));
		}
		
	}
}
Global site tag (gtag.js) - Google Analytics