标签:static nbsp getname 对象 sts his main public set
public class Student {
private String name;
private String sex;
private int age;
// 重写equals
public boolean equals(Object o) {
if (this == o) { //先判断内存地址
return true;
}
if (!(o instanceof Student)) { //判断类型
return false;
}
// 向下转型
Student student = (Student) o;
if (this.age == student.getAge() && this.name.equals(student.getName())
&& this.sex.equals(student.getSex())) {
return true;
} else {
return false;
}
}
public Student(String name, String sex, int age) {
super();
this.name = name;
this.sex = sex;
this.age = age;
}
public Student() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Student类
public class TestStudent {
public static void main(String[] args) {
Student stu1 = new Student("小黑", "男", 50);
Student stu2 = new Student("小黑", "男", 50);
Student stu3 = stu2;
System.out.println(stu1 == stu2); // false
System.out.println(stu2 == stu3);
// 重写我们equals 只要是 student类中的各个属性相等 我们就让两个对象相等
System.out.println(stu2.equals(stu1)); //true
}
}
测试类代码
标签:static nbsp getname 对象 sts his main public set
原文地址:http://www.cnblogs.com/94lkp/p/6197951.html