public class Person {
public static void main(String[] args) { //主方法
Student s = new Student("张三",23);
Student s1 = new Student("张三",23);
boolean b = s.equals(s1); // s.equals(s1);相當于s調用equals()方法,s1賦值equals
System.out.println(b);
System.out.println(s.getName() + "..." + s.getAge());
}
}
public class Student {
@Override
public boolean equals(Object obj) {
//father f = new son(); object obj =s1=new Student("张三",23); 相当于是多态,父类引用指向子类
Student s = (Student)obj; //向下转型
//this相当于s,
return this.name.equals(s.name) && this.age == s.age;
}
private String name;
private int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
}
|
|