Java 创建数组、继承

Java 创建数组、继承,java,arrays,inheritance,Java,Arrays,Inheritance,我正在为即将到来的考试而学习,正在处理一些样例问题,我被困在问题的第四部分。下面是到目前为止的代码,并在中注释了问题 /*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 p

我正在为即将到来的考试而学习,正在处理一些样例问题,我被困在问题的第四部分。下面是到目前为止的代码,并在中注释了问题

/*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]); 
 } 
}   
我得到一个奇怪的输出:

员工详细信息:Employee@4e82701e

员工详细信息:Tradesman@558ee9d6

员工详细信息:Employee@199a0c7c

员工详细信息:Tradesman@50a9ae05

员工详细信息:Employee@33dff3a2

员工详细信息:Tradesman@33f42b49

员工详细信息:Tradesman@6345e044

员工详细信息:Tradesman@86c347

员工详细信息:Employee@f7e6a96


如果您对此有任何建议,以及对上述实施的任何提示/改进,我们将不胜感激。

您将需要一个拥有
员工集合的班级
员工

/* 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]); 
 } 
}   
下面是一些用作伪代码的
groovy
代码(因此您不能只复制它:-)


继续你自己的例子

public class Staff {

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

    public Staff() {
    }

    public void hire() {
    }
}
<>但是你应该考虑使用<代码> ARAYLIST/<代码>,因为这样可以更容易地操作集合。但我不确定你的老师是否允许。否则会是这样的:

public class Staff {

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

    public Staff() {
    }

    public void hire() {
    }
}
Tradesman
执行相同的操作,但首先调用'super.toString(),然后添加此类中的额外字段

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

看到了吗?员工应该是员工的集合。那么,您为什么要从
商人
扩展它呢?

您必须考虑三件事:

您不必对
员工使用继承。它应该是一个包含
Employee
s的
集合的类

由于
Tradesman
是从
Employee
继承而来的,您不必担心将此类对象添加到该集合中。。可以从该集合中的两个类中进行实例化。


而且,您需要有一些函数来将对象添加到集合中或从集合中移除对象。我相信你需要使用
LinkedList
来进行
大学学习

为现实世界做准备!“通过编译检查”:-)你应该问问自己
Staff
是否是一名
Tradesman
Staff
是否拥有一批
员工。现在你有了前者。谢谢,我刚开始创建对象数组,所以有点困难,我已经在上面编辑过了。
Staff
构造函数中的
Employee[]
必须是一个实例变量,否则它在构造后会“消失”。使用
。对于字符串,使用
.equals(…)
而不是
=
返回(name==e.name&&department==e.department);
:name.equals(e.name)…谢谢maba,我可以雇佣、解雇、打印(我想)。它创建的对象数组让我很困惑,我是否应该有一个无参数的构造函数人员()它声明了一个employee类型的对象数组?再次感谢,尽管我得到了一个奇怪的输出,但我想我马上就到了。请参见我上面的编辑。您应该重写
toString()
方法。然后它将打印您想要的内容。现在它正在打印
object.toString()
方法。
public class Staff {

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

    public Staff() {
    }

    public void hire() {
    }
}
public class Employee {
    ... // all your code

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