public class Student {
private String name; private int age; public boolean equals(Object o) {
boolean flag = false;
if (o != null) {
if (this == o) {
flag = true;
} else {
if (o instanceof Student) {
Student s = (Student) o;
if (this.name.equals(s.name) && this.age == s.age) {
flag = true;
}
}
}
}
return flag;
} public static void main(String[] args) {
Student s1 = new Student();
s1.name = "kaka";
s1.age = 10;
Student s2 = new Student();
s2.name = "kaka";
s2.age = 10;
System.out.println(s1.equals(s2));
}
}

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【skyearthsea】截止到2008-07-14 14:06:03的历史汇总数据(不包括此帖):
    发帖的总数量:29                       发帖的总分数:480                      每贴平均分数:16                       
    回帖的总数量:23                       得分贴总数量:2                        回帖的得分率:8%                       
    结贴的总数量:28                       结贴的总分数:470                      
    无满意结贴数:4                        无满意结贴分:60                       
    未结的帖子数:1                        未结的总分数:10                       
    结贴的百分比:96.55 %               结分的百分比:97.92 %                  
    无满意结贴率:14.29 %               无满意结分率:12.77 %                  
    值得尊敬
      

  2.   

    重写了equals最好将你的hashcode方法也一起重写。
      

  3.   

    天  没看懂
    if (o != null) { //如果不为空就XXXXX
    .....           
     } else {    //为空的时候就XXXXX
      .....
    }
    问题好多 
      

  4.   

    直接return 不好吗为什么定义个变量  最后才return?
      

  5.   

    这是规范 最好有一个return,先不关心这个
      

  6.   

    逻辑合并了一下
    public class Student
    {
        private String name;    private int age;    public boolean equals(Object o)
        {
            boolean flag = false;        if(o != null)
            {
                if(this == o)
                {
                    flag = true;
                }
                else if(o instanceof Student)
                {
                    Student s = (Student)o;                if(this.name.equals(s.name) && this.age == s.age)
                    {
                        flag = true;
                    }
                }
            }
            
            return flag;
        }    public static void main(String[] args)
        {
            Student s1 = new Student();
            s1.name = "kaka";
            s1.age = 10;
            Student s2 = new Student();
            s2.name = "kaka";
            s2.age = 10;
            System.out.println(s1.equals(s2));
        }
    }
      

  7.   


    一看都是规范没学好的,hoho
      

  8.   

    不好意思   看错大括号了我看这种代码样式的  有时候迷糊   xxxxx{
          xxxxxx{
         }
          xxxxxx{
         }
      }
         
      

  9.   


    代码也有改动好不好楼主的是
        if()
        {
            
        }
        else
        {
            if()
            {
                
            }
        }
    我改的是
        if()
        {
            
        }
        else if()
        {
            
        }
      

  10.   

    public boolean equals(Object obj) {
    if (obj == this)
    return true;
    if (!(obj instanceof Student))
    return false;
    Student s = (Student) obj;
    return this.name.equals(s.name) && this.age == s.age;
    }
      

  11.   

    用o.getClass()==Student.class 会不会更精确一点
      

  12.   


    还可以更好一点
    public boolean equals(Object obj) {
      if (this == obj) return true;
      
      if (obj == null) return false;  if (getClass() != obj.getClass()) return false; 
      //注意,这里如果用 (obj instanceof Student) 来判断的话,那么Student所有的子类
      //的 equals 方法就不能有改变  Student s = (Student) obj;  return name.equals(s.name) && age == s.age;  }
      

  13.   

    这样写会抛空指针吧?
    if(this.name.equals(s.name) && this.age == s.age)
                    {
                        flag = true;
                    }
    要是我就先判断一下
      

  14.   

    重写equals,就必须重写hashCode.要不比较对象时候,不正确。
    如Map添加内容时的equeals。
      

  15.   

    public boolean equals(Object obj) {
    if (obj == this) return true;
    if (obj == null) return false;
    if (obj.getClass() != this.getClass()) return false;
    Student other = (Student) obj;
    return (other.name == null ? this.name == null : other.name.equals(this.name)) &&
           (other.age == this.age);
    }