gpt4 book ai didi

java - 类 创建方法时的返回类型

转载 作者:行者123 更新时间:2023-12-01 09:22:48 25 4
gpt4 key购买 nike

[UML图][1]

valid XHTML

我正在准备下周的期中考试,我正在练习教授给出的一些例子;但是,我在类返回类型方法方面遇到了一些麻烦。

为了以防万一,我附上了 UML 图。

我想要理解的是 Job 类中的 getPerson 方法。我认为我不需要 Job 类中的数组列表来存储所有员工。因为我已经在 Company 类中有一个数组列表。另外,返回类型是 Employee 类,我不确定如何使用此类返回类型获取人员信息。

我的问题

  1. Job 类中的 public Employee getPerson() {}
  2. Job 类中的 public boolean isVacant() {}
  3. 另外,您介意检查一下 getVacantJobs、getFilledJobs 和 getAllJobs 方法是否正确构建吗?

我使用迭代器来显示所有存储的作业。

----------------------------------------员工类-------------------- -----------

public class Employee {
private String name;
private int id;

public Employee(int id, String name) {
this.name = name;
this.id =id;

}

public final String getName() {
return name;
}

public final void setName(String name) {
this.name = name;
}

public final int getId() {
return id;
}

public final void setId(int id) {
this.id = id;
}

@Override
public String toString() {
return "Employee [name=" + name + ", id=" + id + "]";
}

}

----------------------------------------职位类别----------------- ----------------------------------

public class Job {

private String description;

private int id;
private double maxSalary;

public Job(int id, double maxSalary, String description) {
this.description = description;
this.id = id;
this.maxSalary = maxSalary;
}

public Job(int id, double maxSalary, String description, Employee e1) {
this.description = description;
this.id = id;
this.maxSalary = maxSalary;



}

@Override
public String toString() {

return "Job [description=" + description + ", id=" + id
+ ", maxSalary=" + maxSalary + "]";
}

public final String getDescription() {
return description;
}

public final void setDescription(String description) {
this.description = description;
}

public final double getMaxSalary() {
return maxSalary;
}

public final void setMaxSalary(double maxSalary) {
this.maxSalary = maxSalary;
}

public final int getId() {
return id;
}

public Employee getPerson() {
retrun
}

public final void setPerson(Employee person) {
this.id = person.getId();

}
}

--------------------------公司类-------------------- --------

import java.util.ArrayList;
import java.util.Iterator;

public class Company {

static ArrayList list = new ArrayList();
Iterator itr = list.iterator();


private String name;

public Company(String name) {
this.name = name;
}

public Company() {
// TODO Auto-generated constructor stub
}

public static void addJob(Job j1) {
list.add(j1);
}

public void removeJob(int id) {
list.remove(id);

}


public ArrayList<Job> getVacantJobs() {
while (itr.hasNext()) {
if ((itr == null)) {
System.out.println(itr);
}
}
return null;

}

public ArrayList<Job> getFilledJobs() {
while (itr.hasNext()) {
if (!(itr == null)) {
System.out.println(itr);
}
}
return null;
}

public ArrayList<Job> getAllJobs() {
while (itr.hasNext()) {
System.out.println(itr.next());
}
return null;
}

}

最佳答案

将字段person添加到Job类。

public class Job {

// .....

private Employee person;

public Employee getPerson() {
return person;
}

public final void setPerson(Employee person) {
this.person = person;
}

public boolean isVacant() {
return person == null;
}
}

并将 jobs 字段添加到 Company 类中。

public class Company {

// static ArrayList list = new ArrayList(); // You don't need this
// Iterator itr = list.iterator(); // You don't need this.

// .....

private ArrayList<Job> jobs = new ArrayList<>();

public ArrayList<Job> getVacantJobs() {
ArrayList<Job> result = new ArrayList<>();
for (Job job : jobs)
if (job.isVacant())
result.add(job);
return result;
}

public ArrayList<Job> getFilledJobs() {
ArrayList<Job> result = new ArrayList<>();
for (Job job : jobs)
if (!job.isVacant())
result.add(job);
return result;
}

public ArrayList<Job> getAllJobs() {
ArrayList<Job> result = new ArrayList<>();
for (Job job : jobs)
result.add(job);
return result;
}
}

关于java - 类 创建方法时的返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40050935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com