使用存储在Enum中的值作为字符串文字的最佳方法是什么?例如:
public enum Modes {
some-really-long-string,
mode1,
mode2,
mode3
}
Run Code Online (Sandbox Code Playgroud)
然后我可以用它Mode.mode1
来返回它的字符串表示mode1
.无需继续通话Mode.mode1.toString()
.
我正在完成一项任务,我必须:
使用以下属性/变量创建Employee类:name age department
创建一个名为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()); …
Run Code Online (Sandbox Code Playgroud)