简体   繁体   English

Java中的方法实现

[英]Method Implementation in Java

How do I add input to the HashSet in the given problem. 如何在给定问题中将输入添加到HashSet中。 I mainly want to use the addEmployee, removeEmployee, printEmployee methods in the Manager class, but I can't figure out what to write in the main method so that the addEmployee, removeEmployee and printEmployee methods of Manager class are invoked 我主要想在Manager类中使用addEmployee,removeEmployee,printEmployee方法,但是我不知道要在main方法中写什么,因此无法调用Manager类的addEmployee,removeEmployee和printEmployee方法。

public class EmployeeTest
{
   public static void main(String[] args) 
   {
      Manager mgr = new Manager(207, "Barbara Johnson", "054-12-2367", 109_501.36, "US Marketing");
      Employee e=new Employee(127, "Kyle Jenner", "023-42-5368", 123_243.90); // newly edited
      mgr.addEmployee(e); //newly edited
      mgr.printEmployee(e); //newly edited
      printEmployee(mgr);
   }
   public static void printEmployee(Employee emp) 
   {
      System.out.println(); 
      System.out.println("Employee id:         " + emp.getEmpId());
      System.out.println("Employee name:       " + emp.getName());
      System.out.println("Employee Soc Sec #:  " + emp.getSsn());
      System.out.println("Employee salary:     " + NumberFormat.getCurrencyInstance().format((double) emp.getSalary()));          
   }
}

public class Employee 
{
  private int Id;
  private String name;
  private String ssn;
  private double salary;
  public Employee(int Id, String name, String ssn, double salary)
  {
     this.Id = Id;
     this.name = name;
     this.ssn = ssn;
     this.salary = salary;
  }
  public int getEmpId() 
  {
     return Id;
  }
  public String getName() 
  {
     return name;
  }
  public String getSsn()
  {
     return ssn;
  }
  public double getSalary() 
  {
     return salary;
  }
  public void setName(String name) 
  {
     if (name != null && !name.equals("")) 
     {
         this.name = name;
     }
  }
  public void raiseSalary(double increase) 
  {
     if (increase > 0) 
     {
         salary += increase;

     }
  }
}

public class Manager extends Employee 
{
  private String dept;
  public Manager(int Id, String name, String ssn, double salary, String dept)
  {
     super(Id, name, ssn, salary);
     this.dept = dept;
  }
  **private Set<Employee> staff=new HashSet<>();**  //This is where I need help
  **public void addEmployee(Employee e)**  //This is where I need help
  {
     staff.add(e);
  }
  **public void removeEmployee(Employee e)**  //This is where I need help
  {
     staff.remove(e);
  }
  **public void printEmployee(Employee e)**  //This is where I need help
  {
     for (Employee emp: staff)
     {
        System.out.println("Name : "+getName()+" "+"ID : "+getEmpId());
     }
  }
  public String getDeptName()
  {
     return dept;
  }
}

Output Of Program 程序输出

Name : Barbara Johnson ID : 207 //Desired Output:- Name : Kyle Jenner ID : 127 名称:芭芭拉·约翰逊(Barbara Johnson)ID:207 //所需输出:-名称:Kyle Jenner ID:127

Employee id: 207 员工编号:207

Employee name: Barbara Johnson 员工姓名:芭芭拉·约翰逊(Barbara Johnson)

Employee Soc Sec #: 054-12-2367 员工俱乐部编号:054-12-2367

Employee salary: Rs.109,501.36 员工薪水:109,501.36卢比

Testing raiseSalary and setName on Manager: 在Manager上测试raiseSalary和setName:

Employee id: 207 员工编号:207

Employee name: Barbara Johnson-Smythe 员工姓名:芭芭拉·约翰逊·史密斯

Employee Soc Sec #: 054-12-2367 员工俱乐部编号:054-12-2367

Employee salary: Rs.119,501.36 员工薪水:119,501.36卢比

Since the static type of emp in printEmployee() is Employee you can use only the method which are defined in Employee (or defined there and overriden in Manager ). 由于printEmployee()中emp的静态类型为Employee您只能使用Employee中定义的方法(或在Employee定义并在Manager覆盖的方法)。 Since printEmployee() is defined in Manager it can be used only in variables declaed as Manager 由于printEmployee()是在Manager定义的,因此只能在声明为Manager变量中使用

Your output shows the managers name because Manager.printEmployee() is calling get getName() and getEmpID() on the Manager object, not on its employees. 您的输出显示经理姓名,因为Manager.printEmployee()Manager对象而不是在其雇员上调用get getName()getEmpID()

**public void printEmployee(Employee e)**  //This is where I need help
{
  for (Employee emp: staff)
  {
     System.out.println("Name : "+getName()+" "+"ID : "+getEmpId()); 
  } 
}

(In addition, this method receives a reference e to an Employee object that it then ignores.) (此外,此方法接收对Employee对象的引用e ,然后该对象将忽略该对象。)

Remember how you modified printEmployee(e); 记住您如何修改printEmployee(e); to mgr.printEmployee(e); mgr.printEmployee(e); to call the method on the Manager object referenced by mgr? 调用由mgr引用的Manager对象上的方法? You'll need to do the same thing to call the method's on the Employee objects. 您需要做同样的事情才能在Employee对象上调用方法。

public class Manager extends Employee {
   **public void printEmployee()**  //This is where I need help
   {
      for (Employee emp: staff)
      {
         System.out.println("Name : "+emp.getName()+" "+"ID : "+emp.getEmpId()); 
      } 
   }
   ...

Here's the key thing to remember. 这是要记住的关键事项。 You're not just calling a set of functions. 您不仅在调用一组函数。 You're interacting with objects by calling methods on them. 您正在通过在对象上调用方法来与它们进行交互。 If you want to interact with an object, you have to call methods through a reference to that object -- as you've done with mgr.addEmployee() in the main() method. 如果要与对象进行交互,则必须通过对该对象的引用来调用方法, mgr.addEmployee()在main()方法中使用mgr.addEmployee()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM