我的工作分配中,我有:
创建具有以下属性/变量的Employee类:名称年龄部门
创建一个名为Department类其中将包含员工的列表。
一个。 Department类将有将返回其员工按年龄排序的方法。
乙。系数值只能是下列之一:
- 会计学
- 市场营销
- 人力资源
- 信息系统
我有困难的时候试图找出如何完成2B。以下是我迄今为止:
进口的java.util。*;公共类Employee {字符串名称;INT年龄;串部门;员工(字符串名称,诠释年龄,弦乐部){ this.name =名称; this.age =年龄; this.department =部;}INT getAge(){ 返回年龄;}}类系{公共静态无效的主要(字串[] args){ 清单<员工> empList =新的ArrayList<员工>(); Collections.sort(empList,新的比较<员工>(){ 公众诠释比较(员工E1,E2员工){ 返回新的整数(e1.getAge())的compareTo(e2.getAge()); } }); }}解决方案
您可以使用,这将限制你只能使用指定的值相同的目的枚举。声明你的处枚举如下:
公共枚举系{ 会计,Marketting,Human_Resources,Information_Systems}现在
您员工类可以是
公共类Employee { 字符串名称; INT年龄; 系部; 员工(字符串名称,诠释年龄,系部){ this.name =名称; this.age =年龄; this.department =部; } INT getAge(){ 返回年龄; }}和同时创造员工,你可以使用
员工员工=新员工(普拉萨德,47,Department.Information_Systems);修改按阿德里安深当然,因为它是一个伟大的建议的建议。
- 的枚举是这就是为什么其良好的大写字母根据Java约定声明常量。
- 但是,我们不希望枚举的资本重新presentation待观察,所以我们可以创建枚举构造,并通过读取信息给它。
-
我们西港岛线修改枚举包括的toString()方法和构造,它接受一个字符串参数。
公共枚举系{ 会计(会计),MARKETTING(Marketting),HUMAN_RESOURCES( 人力资源),INFORMATION_SYSTEMS(信息系统); 私人字符串DEPTNAME; 部(字符串DEPTNAME){ this.deptName = DEPTNAME; } @覆盖 公共字符串的toString(){ 返回this.deptName; }}
所以,当我们正在创建一个员工对象,如下所示,使用它
员工员工=新员工(普拉萨德Kharkar,47,Department.INFORMATION_SYSTEMS);的System.out.println(employee.getDepartment());我们会得到一个可读的字符串重新presentation为信息系统因为它是由返回的toString()方法,它是通过的System.out.println()语句。阅读关于枚举
I am working on an assignment in which I have to:
Create an Employee class with the following attributes/variables: name age department
Create a class called Department which will contain a list of employees.
a. Department class will have a method which will return its employees ordered by age.
b. Value of Department can be only one of the following:
- "Accounting"
- "Marketing"
- "Human Resources"
- "Information Systems"
I am having the hardest time trying to figure out how to complete 2b. Here is what I have so far:
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())pareTo(e2.getAge()); } }); } }解决方案
You can use enumerations for the same purpose which will restrict you to use only specified values. Declare your Department enum as follows
public enum Department { Accounting, Marketting, Human_Resources, Information_Systems }You Employee class can now be
public class Employee { String name; int age; Department department; Employee(String name, int age, Department department) { this.name = name; this.age = age; this.department = department; } int getAge() { return age; } }and while creating employee, you can use
Employee employee = new Employee("Prasad", 47, Department.Information_Systems);EDIT as suggested by Adrian Shum and of course because it is a great suggestion.
- The enums are constants thats why its good to be declared in capital letters according to java conventions.
- But we don't want the capital representation of the enums to be seen so we can create enum constructors and pass readable info to it.
We wil modify enum to include toString() method and constructor which takes a string argument.
public enum Department { ACCOUNTING("Accounting"), MARKETTING("Marketting"), HUMAN_RESOURCES( "Human Resources"), INFORMATION_SYSTEMS("Information Systems"); private String deptName; Department(String deptName) { this.deptName = deptName; } @Override public String toString() { return this.deptName; } }
So when we are creating an Employee object as follows and using it,
Employee employee = new Employee("Prasad Kharkar", 47, Department.INFORMATION_SYSTEMS); System.out.println(employee.getDepartment());We will get a readable string representation as Information Systems as it is returned by toString() method which is called implicitly by System.out.println() statement. Read the good tutorial about Enumerations
更多推荐
如何设置一个对象的变量的值在一个ArrayList
发布评论