0

我正在为即将到来的考试学习并解决示例问题,但被困在问题的第四部分。到目前为止的代码如下,其中评论了问题。

/*Part 1. Write a class Employee to encapsulate the notion of an employee. 
The class should include a name and a department (both of type String), 
an all-args constructor, and a method to print the details of the employee on the screen. 
It should also provide an appropriate equals method 
(equality of employees requires that name and department be the same). 
Check it by compiling. */

public class Employee {

public String name;
public String department;

public Employee(String n, String d) {
    name = n;
    department = d;
}

public void print() {
    System.out.println("Employee's name: " + name);
    System.out.println("Employee's department: " + department);
}

public boolean equals(Employee e) {
    return(name==e.name&&department==e.department);
}
}

第二部分

/* A tradesman is an employee with a trade such as “carpenter” or “painter”. 
Write a class Tradesman to encapsulate the notion of a tradesman. 
It should include a method to print out the tradesman’s details. 
Make the class by inheritance from Employee. 
An employee’s trade is not significant for equality. 
Check the class by compiling. */

public class Tradesman extends Employee {

public String trade;

public Tradesman(String n, String d, String t) {
    super(n, d);
    trade = t;
}

public void setTrade (String newTrade) {
    trade = newTrade;
}

public void print() {
    System.out.println("Tradesmans name: " + name);
    System.out.println("Tradesmans department: " + department);
    System.out.println("Tradesmans trade: " + trade);
}
}

下一部分是我卡住的地方,我知道我应该有一个构造函数 Staff() 来创建一个数组,我可以在其中存储员工和商人我只是不知道如何去做。所有指针表示赞赏。

/* A staff is a collection of employees, some of whom are tradesmen. 
Write a class Staff to encapsulate the notion of a staff. 
The members of staff should be stored in an array (which will typically not be full). 
Include methods hire to hire and fire to fire a staff member, 
as well as method put to print out a complete list of the staff members. 
Check it by compiling.*/

import java.util.ArrayList;
import java.util.Arrays;

public class Staff {

private ArrayList emp = new ArrayList();

public Staff() {
}

public void hire(Employee employee) {
    emp.add(employee);
}

public void fire(Employee employee) {
    emp.remove(employee);
}

public void put() {
    // get array 
Object ia[] = emp.toArray(); 
for(int i=0; i<ia.length; i++) 
System.out.println("Employee details: " + ia[i]); 
 } 
}   

正在使用以下程序进行测试:

class StaffTest { 

public static void main(String[] args) {  
Staff personnel = new Staff();
    Employee e1 = new Employee("Mike","Sales");
    Employee e2 = new Tradesman(
                    "Fred","Engineering","Welder");
    Employee e3 = new Employee("Pat","Sales");
    Employee e4 = new Tradesman(
                    "Jean","Finishing", "Painter");
    Employee e5 = new Employee("Bill","Marketing");
    Employee e6 = new Tradesman(
                    "Anne","Engineering", "Fitter");
    Employee e7 = new Tradesman(
                    "Paul","Design", "Draughtsman");
    Employee e8 = new Tradesman(
                    "Eddy","Finishing","Painter");
    Employee e9 = new Employee("John","Despatch"); 
    personnel.hire(e1); personnel.hire(e2); 
    personnel.hire(e3); personnel.hire(e4); 
    personnel.hire(e5); personnel.hire(e6);
    personnel.hire(e7); personnel.hire(e8); 
    personnel.hire(e9); 
    personnel.put(); System.out.println();
    personnel.fire(e1); personnel.fire(e5); 
    personnel.fire(e9);
    personnel.put(); System.out.println();
    personnel.fire(new Tradesman(
                    "Eddy", "Finishing", "Painter"));               
    personnel.put(); 
}
}

我得到一个奇怪的输出:

员工详细信息:Employee@4e82701e

员工详细信息:Tradesman@558ee9d6

员工详细信息:Employee@199a0c7c

员工详细信息:Tradesman@50a9ae05

员工详细信息:Employee@33dff3a2

员工详细信息:Tradesman@33f42b49

员工详细信息:Tradesman@6345e044

员工详细信息:Tradesman@86c347

员工详细信息:Employee@f7e6a96

对此的任何建议以及对上述实施的任何提示/改进将不胜感激。

4

2 回答 2

0

您将需要一个Staff包含以下集合的类Employees

这是一些groovy用作伪代码的代码(所以你不能只是复制它:-))

class Staff {

    def members = [:] // A collection

    void hire(Employee employee) {
        // Add to collection
    }

    void fire(Employee employee) {
        // Remove from collection
    }

    void print() {
        // iterate over collection and print content
    }
}

去你自己的例子......

public class Staff {

    private Employee[] emp = new Employee[100];

    public Staff() {
    }

    public void hire() {
    }
}

但是您应该考虑使用 anArrayList代替,因为您可以通过这种方式更轻松地操作集合。但我不确定你的老师是否允许这样做。否则它会是这样的:

public class Staff {

    private ArrayList<Employee> emp = new ArrayList<Employee>();

    public Staff() {
    }

    public void hire() {
    }
}

将此添加到您的Employee课程中:

public class Employee {
    ... // all your code

    @Override
    public String toString() {
        // Create a string by using StringBuilder.append() and return sb.toString();
    }
}

做同样的事情,Tradesman但首先调用 `super.toString() 并从这个类中添加额外的字段。

于 2012-08-21T09:39:49.883 回答
0

员工是员工的集合,其中一些是商人。

看?员工应该是员工的集合。那么你为什么要扩展它tradesman呢?

您必须考虑三件事:

您不必为Staff. 它应该是一个包含 a collectionof Employees 的类。

Tradesman继承自您Employee不需要担心将此类对象添加到该集合中。您可以从该集合中的两个类中进行实例化。

而且,您需要一些函数来将对象添加到该集合中/从该集合中移除对象。我相信您需要为此LinkedList使用collecetion.

于 2012-08-21T09:40:11.110 回答