编辑: 我看到我对原始问题的措词有误,应该有2个数组,为了更清楚起见,我对其进行了修改。 除了部门和位置的getter方法外,我现在一切正常。我尝试了多种方法来显示此内容,但是无论我尝试什么,它只会显示地址。这就是我所拥有的(位置相同,只是在具有不同名称的另一个对象中):
public void getDepartments () {
for(int i = 0; i < departments.size(); i++) {
System.out.println(departments.get(i));
}
}
这适用于“雇员”阵列列表,但不适用于部门或位置阵列列表。
从事这个项目,老实说,我不知道该怎么做。本质上是这样分解的:
使用2个变量创建object1(让我们说这是个人,以姓名和年龄作为变量)。
将object2创建为object1的子类,并带有2个其他变量(让我们称其为一名雇员,其变量为雇员ID和职位)
使用名称变量和一个object2数组创建object3(因此名称为部门,该数组将容纳在那里工作的所有员工)
创建一个object4,其中包含一个object3数组。 (这是一系列办公地点)
基本上,我应该能够显示办公室位置的列表,该位置的部门列表以及这些部门的员工列表。
这里是我编写的代码:
对象1:
public class Person {
private String name;
private int age;
public Person () {
name = "";
age = 0;
}
public Person (String name, int age) {
this.name = name;
this.age = age;
}
public void setName (String name) {
this.name = name;
}
public String getName () {
return name;
}
public void setAge (int age) {
this.age = age;
}
public int getAge () {
return age;
}
public String toString () {
return ("Name: " + name + ", Age: " + age);
}
}
对象2:
public class Employee extends Person {
private String employeeID;
private String position;
public Employee (){
super ();
employeeID = "";
position = "";
}
public Student (String name, int age, String employeeID, String position){
super (name, age);
this.employeeID = employeeID;
this.position = position;
}
public void setEmployeeID (String employeeID) {
this.employeeID = employeeID;
}
public String getEmployeeID () {
return employeeID;
}
public void setPosition (String position) {
this.year = position;
}
public String getPosition() {
return year;
}
public String toString () {
return (super.toString() + ", Employee ID: " + employeeID + ", Position:
" + position);
}
}
对象3 :(此时显然缺少很多东西)
public class Department {
private String departmentName;
private int numberOfEmployees;
private Employee[] employees;
public Department() {
}
public Department (String departmentName, int numberOfEmployees) {
this.departmentName = departmentName;
this.numberOfEmployees = numberOfEmployees;
Employee[] employees = new Employee[numberOfEmployees];
}
public String getName() {
return departmentName;
}
public void setName(String departmentName) {
this.departmentName = departmentName;
}
public int getNumberOfEmployees() {
return numberOfEmployees;
}
public void setNumberOfEmployees(int numberOfEmployees) {
this.numberOfEmployees = numberOfEmployees;
}
}
答案 0 :(得分:0)
您应该仔细阅读OOP原则。这是基本的,但对于您的逻辑思想来说确实很重要。
我只是帮助您澄清您的问题。
您需要定义一个Person
类,例如(对于object1)
public abstract class Person {
protected String name;
protected Integer age;
public Person(String n, Integer a){
name = n;
age = a;
}
@Override
public String toString() {
//print out person's info
}
//TODO: you should create getter/setter by yourself.
}
下一个将是Employee
的子类的Person
类:
public class Employee extends Person {
private String employeeId;
private String position;
public Employee (String name, int age, String id, String pos) {
//parent constructor
super(name, age);
employeeId = id;
position = pos;
}
@Override
public String toString() {
//print out employee's info
}
//TODO: you should create getter/setter by yourself.
}
第三位将是Office
:
public class Office {
private String location;
//you can use Person here because it is parent of Employee
private List<Person> employees;
//TODO: you should create getter/setter by yourself.
public Office(String loc) {
location = loc;
employees = new ArrayList<Person>();
}
public void addEmployee(Person p) {
employees.add(p);
}
}
然后在您的main
类中,您可以这样声明对象:
Person p1 = new Employee("Mr.X", 30, "ID001", "Manager");
Person p2 = new Employee("Mr.Y", 31, "ID002", "Director");
Office office = new Office("Silicon Valley");
office.addEmployee(p1);
office.addEmployee(p2);
List<Office> offices =new ArrayList<Office>();
offices.add(office);