学生类
class Student {
private String name;
private int age;
private String sex;
public Student() {
super();
}
public Student(String name, int age, String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void show(){
System.out.println(name+age+sex);
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (sex == null) {
if (other.sex != null)
return false;
} else if (!sex.equals(other.sex))
return false;
return true;
}
}
测试类
public class StudentTest {
/**
* @param args
*/
public static void main(String[] args) {
Student s1 = new Student("s2",11, "男");
Student s2 = new Student("s2",11,"男");
System.out.println(s1.equals(s2));
}
}
学生类的equals方法谁懂啊,?希望大牛们给详细说明一下,还有s1.equals(s2)这个方法是如何调用的呢?怎么解释啊
|
|