博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

equals()方法

Posted on 2011-11-30 15:12  酸梅拯救地球  阅读(178)  评论(0)    收藏  举报
class Student
{
	String name;
	int id;
	Student(String name,int id){
		this.name=name;
		this.id=id;
	}
	public boolean equals(Object o){
	       if(this==o) return true;
	    if(o instanceof Object)
	    {
	    	boolean temp=(this.id == ((Student)o).id);
	        return (this.id == ((Student)o).id) && (this.name.equals(((Student)o).name));
	                                               //String类的equals()方法是重载的equals方法  只有对象数据类型有equals方法
	    }                                                                       
                                                      //对于this.id ==((Person)o).id)结果是true	
		return super.equals(o);
	    }	
}