本帖最后由 赵宇 于 2012-9-25 08:43 编辑
public class EqualsTest
{
public static void main(String[] args)
{
Student s1 = new Student("zhangsan");
Student s2 = new Student("zhangsan");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
}
class Student
{
String name;
public Student(String name)
{
this.name = name;
}
public boolean equals(Object anObject) (此处为重写equals方法)
{
if(this == anObject)
{
return true;
}
if(anObject instanceof Student)
{
Student student = (Student)anObject;
if(student.name.equals(this.name)) (从此处的 this.name的值是什么?)
{
return true;
}
}
return false;
}
}
|