当有一系列人(员工和学生的实例化)时

发布于 2025-01-31 05:33:44 字数 2958 浏览 3 评论 0原文

我有很多人。我已经实例化了子类 员工两次,一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文