问题
重写equals方法时,不要使用instanceof判断,为什么呢?
解析
因为当一个类的子类和他进行比较时,用instanceof方法也会返回true
public class Manager extends Employee() {}
public class Employee() {}
public class Test(){
public static void main(String[] args) {
Manager m = new Manager();
System.out.println(m instanceof Employee);
}
}
那么就只用getClass的方法了
public class Test(){
public static void main(String[] args) {
Manager m = new Manager();
Employee e = new Employee();
System.out.println(m.getClass() == e.getClass());
}
}