当有一系列人(员工和学生的实例化)时
我有很多人。我已经实例化了子类 员工
两次,一个 student 子类。我只想知道为什么我会因为我的平等方法而言是错误的。此代码的大部分来自Core Java卷1基础 ,我正在阅读本书以获取复习,但不知道该如何处理等于方法。是因为它是Person
class 。首先,我尝试将其他名称和工资放在人数阵列中的3人身上。我还在陷入虚假。然后我将其切换到同一名称,但仍然有错误。我并不总是了解调试工具,但我认为我的返回声明似乎是问题所在。
package v1ch05.abstractClasses;
public class PersonTest
{
public static void main(String[] args)
{
var people = new Person[3];
// fill the people array with Student and Employee objects
people[0] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
people[1] = new Student("Maria Morris", "computer science");
people[2] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
// print out names and descriptions of all Person objects
for (Person p : people)
System.out.println(p.getName() + ", " + p.getDescription());
boolean isEmployee = people[0].equals(people[2]);
System.out.println("Is Employee:" + isEmployee);
}
}
package v1ch05.abstractClasses;
import java.time.*;
import java.util.Objects;
public class Employee extends Person
{
private double salary;
private LocalDate hireDay;
public Employee(String name, double salary, int year, int month, int day)
{
super(name);
this.salary = salary;
hireDay = LocalDate.of(year, month, day);
}
public double getSalary()
{
return salary;
}
public LocalDate getHireDay()
{
return hireDay;
}
public String getDescription()
{
return String.format("an employee with a salary of $%.2f", salary);
}
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}
@Override
public boolean equals(Object otherObject) {
if(this == otherObject) return true;
if(otherObject == null) return false;
if(getClass() != otherObject.getClass()) return false;
Employee other = (Employee) otherObject;
return super.equals(other)
&& salary == other.salary
&& Objects.equals(hireDay, other.hireDay);
}
}
package v1ch05.abstractClasses;
public abstract class Person
{
public abstract String getDescription();
private String name;
public Person(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
@Override
public boolean equals(Object otherObject) {
if(this == otherObject) return true;
if(otherObject == null) return false;
if(getClass() != otherObject.getClass()) return false;
Person other = (Person) otherObject;
return name == other.name;
}
}
I have an array of people. I have instantiated the subclass Employee
twice and one Student
subclass in the array. I just want to know why I'm getting false when it should say true with regards to my equals method. Most of this code is from the Core Java Volume 1 Fundamentals and I'm reading through the book to get a refresher but have no clue what to do with the equals method. Is it because it's a Person
Class. First I tried putting the putting a different name and salary on the 3 person in the Person Array. I was still getting false. And then I switch it to the same name and still got false. I don't always understand the debugging tool but I thought it looked like the problem was with my return statement.
package v1ch05.abstractClasses;
public class PersonTest
{
public static void main(String[] args)
{
var people = new Person[3];
// fill the people array with Student and Employee objects
people[0] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
people[1] = new Student("Maria Morris", "computer science");
people[2] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
// print out names and descriptions of all Person objects
for (Person p : people)
System.out.println(p.getName() + ", " + p.getDescription());
boolean isEmployee = people[0].equals(people[2]);
System.out.println("Is Employee:" + isEmployee);
}
}
package v1ch05.abstractClasses;
import java.time.*;
import java.util.Objects;
public class Employee extends Person
{
private double salary;
private LocalDate hireDay;
public Employee(String name, double salary, int year, int month, int day)
{
super(name);
this.salary = salary;
hireDay = LocalDate.of(year, month, day);
}
public double getSalary()
{
return salary;
}
public LocalDate getHireDay()
{
return hireDay;
}
public String getDescription()
{
return String.format("an employee with a salary of $%.2f", salary);
}
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}
@Override
public boolean equals(Object otherObject) {
if(this == otherObject) return true;
if(otherObject == null) return false;
if(getClass() != otherObject.getClass()) return false;
Employee other = (Employee) otherObject;
return super.equals(other)
&& salary == other.salary
&& Objects.equals(hireDay, other.hireDay);
}
}
package v1ch05.abstractClasses;
public abstract class Person
{
public abstract String getDescription();
private String name;
public Person(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
@Override
public boolean equals(Object otherObject) {
if(this == otherObject) return true;
if(otherObject == null) return false;
if(getClass() != otherObject.getClass()) return false;
Person other = (Person) otherObject;
return name == other.name;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论