Java 中的方法实现
Method Implementation in Java
如何将输入添加到给定问题中的 HashSet。我主要是想用Manager中的addEmployee,removeEmployee,printEmployee方法class,但是想不通main方法里面写什么,以至于Manager的addEmployee,removeEmployee,printEmployee方法class ] 被调用
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;
}
}
程序输出
姓名:Barbara Johnson ID:207 //期望的输出:- 姓名:Kyle Jenner ID:127
员工编号:207
员工姓名:Barbara Johnson
员工 Soc Sec #:054-12-2367
员工工资:Rs.109,501.36
正在经理上测试 raiseSalary 和 setName:
员工编号:207
员工姓名:Barbara Johnson-Smythe
员工 Soc Sec #:054-12-2367
员工工资:Rs.119,501.36
由于printEmployee()
中emp的静态类型是Employee
,你只能使用Employee
中定义的方法(或在那里定义并在Manager
中覆盖) ).
由于 printEmployee()
是在 Manager
中定义的,因此只能在声明为 Manager
的变量中使用
您的输出显示经理姓名,因为 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());
}
}
(此外,此方法接收对随后忽略的 Employee 对象的引用 e
。)
还记得您如何将 printEmployee(e);
修改为 mgr.printEmployee(e);
以调用 mgr 引用的 Manager 对象上的方法吗?您需要做同样的事情来调用 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());
}
}
...
这是要记住的关键事项。您不只是调用一组函数。您通过调用对象的方法与对象进行交互。如果你想与一个对象交互,你必须通过对该对象的引用来调用方法——就像你在 main() 方法中使用 mgr.addEmployee()
所做的那样。
如何将输入添加到给定问题中的 HashSet。我主要是想用Manager中的addEmployee,removeEmployee,printEmployee方法class,但是想不通main方法里面写什么,以至于Manager的addEmployee,removeEmployee,printEmployee方法class ] 被调用
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;
}
}
程序输出
姓名:Barbara Johnson ID:207 //期望的输出:- 姓名:Kyle Jenner ID:127
员工编号:207
员工姓名:Barbara Johnson
员工 Soc Sec #:054-12-2367
员工工资:Rs.109,501.36
正在经理上测试 raiseSalary 和 setName:
员工编号:207
员工姓名:Barbara Johnson-Smythe
员工 Soc Sec #:054-12-2367
员工工资:Rs.119,501.36
由于printEmployee()
中emp的静态类型是Employee
,你只能使用Employee
中定义的方法(或在那里定义并在Manager
中覆盖) ).
由于 printEmployee()
是在 Manager
中定义的,因此只能在声明为 Manager
您的输出显示经理姓名,因为 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());
}
}
(此外,此方法接收对随后忽略的 Employee 对象的引用 e
。)
还记得您如何将 printEmployee(e);
修改为 mgr.printEmployee(e);
以调用 mgr 引用的 Manager 对象上的方法吗?您需要做同样的事情来调用 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());
}
}
...
这是要记住的关键事项。您不只是调用一组函数。您通过调用对象的方法与对象进行交互。如果你想与一个对象交互,你必须通过对该对象的引用来调用方法——就像你在 main() 方法中使用 mgr.addEmployee()
所做的那样。