目录
题目需求
有
Person
类,Company
类,Employee
类。其中
Employee
类继承自Person
类,属性为:private Company company; private double salary;
现在要求覆盖Employee类的
equals
方法,判定两个Employee对象是否相等,请见如下判断方法:
其继承自父类Person的属性值都相等,其company属性对象equals返回true,且其salary也相等。
salary是double型,比较时,使用
DecimalFormat df = new DecimalFormat("#.##");
使salary保留两位小数,然后再进行比较。注意:要考虑company为
null
的情况。函数接口定义:
public boolean equals(Object obj)
已有的main类定义:
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String name1 = scanner.nextLine(); int age1 = scanner.nextInt(); scanner.nextLine(); String companyName1 = scanner.nextLine(); double salary1 = scanner.nextDouble(); scanner.nextLine(); Company company1 = new Company(companyName1, "Location1"); Employee employee1 = new Employee(name1, age1, company1, salary1); // 重复以上步骤获取第二个员工的信息 String name2 = scanner.nextLine(); int age2 = scanner.nextInt(); scanner.nextLine(); String companyName2 = scanner.nextLine(); double salary2 = scanner.nextDouble(); scanner.nextLine(); Company company2 = new Company(companyName2, "Location2"); Employee employee2 = new Employee(name2, age2, company2, salary2); // 比较两个员工对象是否相等 System.out.println("employee1.equals(employee2): " + employee1.equals(employee2)); // 关闭Scanner scanner.close(); } }
在main方法中输入employ1和employ2两个对象的属性,调用equals方法判断二者是否相等。
覆盖
什么是覆盖
覆盖(override)是面向对象编程中的一个重要概念,它指的是子类重新定义父类中已经定义的方法的行为。当子类继承了一个父类,并且在子类中定义了与父类中同名、同参数列表的方法时,就发生了方法的覆盖。
在Java中,如果子类中定义了一个与父类中同名、同参数列表的方法,那么在使用子类对象调用这个方法时,将会优先调用子类中的方法,而不是父类中的方法。这种覆盖行为允许子类修改或扩展从父类继承而来的方法的行为,使得程序更加灵活和可扩展。
覆盖的规则
覆盖方法的一些重要规则包括:
- 子类中覆盖的方法必须与父类中被覆盖的方法具有相同的方法签名,包括方法名、返回类型和参数列表。
- 覆盖的方法不能降低访问权限,例如不能由public变成protected或private,但可以提高访问权限,例如由protected变成public。
- 如果父类方法被final修饰,那么它不能被覆盖。
- 如果子类中的方法与父类中的方法具有不同的方法签名,则不算是覆盖,而是方法的重载。
通过覆盖,子类能够根据自身的特定需求来重新定义继承自父类的方法,这是实现多态性的重要手段之一。
equals()方法
方法概述
在Java中,equals() 方法是Object类的一个方法,用于比较两个对象是否相等。在Object类中,equals() 方法的默认行为是比较两个对象的引用是否相同,即判断两个对象是否指向内存中的同一块地址。
equals()方法的重写
为何重写
通常情况下我们需要根据对象的内容来判断它们是否相等,这就需要在具体的类中重写 equals() 方法。当我们在一个类中重写 equals() 方法时,我们通常会比较对象的各项属性值是否相等来确定它们是否相等。在重写 equals() 方法时,要注意处理空引用和类型检查,以确保程序的正确性。
重写时的规定
一般来说,重写 equals() 方法需要满足以下规定:
- 自反性:对于任意非空引用值 x,x.equals(x) 应该返回 true。
- 对称性:对于任意非空引用值 x 和 y,如果 x.equals(y) 返回 true,则 y.equals(x) 也应该返回 true。
- 传递性:对于任意非空引用值 x、y 和 z,如果 x.equals(y) 返回 true,并且 y.equals(z) 也返回 true,则 x.equals(z) 也应该返回 true。
- 一致性:对于任意非空引用值 x 和 y,在没有改变的条件下多次调用 x.equals(y) 应该始终返回相同的结果。
- 对于任何非空引用值 x,x.equals(null) 应该返回 false。
思路分析
为了实现equals()方法的覆盖,对引用类型是否相等的判断,以及考虑company为null
的情况,我们显然需要重写equals()方法。在Person类,Company类以及Employ类三个类中,分别重写equals()方法:
- 在重写时,首先检验传入的对象是否为null,如果是的,则直接返回false,因为一个对象不可能与null相等。
- 再判断两对象的引用是否相同,如果是的,则直接返回true。
- 当确定两个对象是同一类型,可以将传入的 Object 对象强制转换为当前类的对象,以便访问对象的属性进行比较。
- 此后,对对象的属性进行逐一比较,并返回结果
完整代码
import java.util.Scanner;
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name1 = scanner.nextLine();
int age1 = scanner.nextInt();
scanner.nextLine();
String companyName1 = scanner.nextLine();
double salary1 = scanner.nextDouble();
scanner.nextLine();
Company company1 = new Company(companyName1, "Location1");
Employee employee1 = new Employee(name1, age1, company1, salary1);
// 重复以上步骤获取第二个员工的信息
String name2 = scanner.nextLine();
int age2 = scanner.nextInt();
scanner.nextLine();
String companyName2 = scanner.nextLine();
double salary2 = scanner.nextDouble();
scanner.nextLine();
Company company2 = new Company(companyName2, "Location2");
Employee employee2 = new Employee(name2, age2, company2, salary2);
// 比较两个员工对象是否相等
System.out.println("employee1.equals(employee2): " + employee1.equals(employee2));
// 关闭Scanner
scanner.close();
}
}
class Company {
private String name;
private String location;
// 构造方法
public Company(String name, String location) {
this.name = name;
this.location = location;
}
// 其他属性和方法
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Company company = (Company) obj;
return name.equals(company.name) && location.equals(company.location);
}
}
class Employee extends Person {
private Company company;
private double salary;
public Employee(String name, int age, Company company2, double v) {
super(name, age);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
if (!super.equals(obj)) {
return false;
}
Employee employee = (Employee) obj;
if (company != null ? !company.equals(employee.company) : employee.company != null) {
return false;
}
DecimalFormat df = new DecimalFormat("#.##");
return Double.compare(Double.parseDouble(df.format(salary)), Double.parseDouble(df.format(employee.salary))) == 0;
}
}
class Person {
private String name;
private int age;
// 构造方法
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// 其他属性和方法
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Person person = (Person) obj;
return age == person.age && (name == null ? person.name == null : name.equals(person.name));
}
}
后言
在上一篇文章,讨论clone()方法时,探讨了浅拷贝与深拷贝的区别,主要是在于对引用类型的处理方式。而在equals()方法中,在比较引用类型时,我们也需要手动重写。可见,对于引用类型的处理,是需要时刻注意的,尤其当两对象引用地址相同时,会产生许多意想不到的错误,导致程序运行不能达到预期的效果。
新人创作,水平有限,如有错误,敬请指出!