【发布时间】:2013-07-10 12:30:24
【问题描述】:
我正在执行一项任务,我必须:
使用以下属性/变量创建一个 Employee 类: 姓名 年龄 部门
-
创建一个名为 Department 的类,该类将包含员工列表。
一个。 Department 类将有一个方法,该方法将返回按年龄排序的员工。
b. Department 的值只能是以下之一:
- “会计”
- “营销”
- “人力资源”
- “信息系统”
我很难弄清楚如何完成 2b。 这是我目前所拥有的:
import java.util.*;
public class Employee {
String name;
int age;
String department;
Employee (String name, int age, String department) {
this.name = name;
this.age = age;
this.department = department;
}
int getAge() {
return age;
}
}
class Department {
public static void main(String[] args) {
List<Employee>empList = new ArrayList<Employee>();
Collections.sort (empList, new Comparator<Employee>() {
public int compare (Employee e1, Employee e2) {
return new Integer (e1.getAge()).compareTo(e2.getAge());
}
});
}
}
【问题讨论】:
-
" b. Value of Department can be only one of the following: "Accounting" "Marketing" "Human Resources" "Information Systems""-- 对此属性使用 enum 类型,而不是字符串。这将强制 Java 只允许此变量或参数类型的特定项。 -
欲了解更多信息,请查看Enum Types
-
下次请仅提供与您的问题相关的信息。整堆代码和你的作业的大部分描述与你的问题无关。