我正在尝试为以下问题编写代码:WAP 以获取少数员工的 empno、DOB 和薪水。编写两个匿名内部类,一个实现了一个接口以根据员工的 DOB 对员工进行排序,另一个匿名内部类扩展了一个类以根据员工的薪水对员工进行排序。我的代码是:
//required import statements
public class OuterClass {
private int empno;
private String DOB;
private int salary;
public OuterClass() {
// TODO Auto-generated constructor stub
}
public OuterClass(int empn, String dob, int sal) {
//code for paramaterized constructor
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<OuterClass> list = new ArrayList<OuterClass>();
while (true) {
//code for input from user and adding it in list
}
}
Employee abc = new Employee() {
@Override
void SalarySort(List<OuterClass> l) {
Collections.sort(l, new Comparator<OuterClass>() {
public int compare(OuterClass e1, OuterClass e2) {
return e1.salary - e2.salary;
}
});
Iterator<OuterClass> itr = l.iterator();
while (itr.hasNext()) {
OuterClass employee = (OuterClass) itr.next();
System.out.println("Emp no : " + employee.empno + " Emp dob : "
+ employee.DOB + " Emp Salary : " + employee.salary);
}
}
}.SalarySort(list);// compile error : create local field list
}
class Employee {
void SalarySort(List<OuterClass> l) {
}
}
我的问题是,我不能调用匿名类Employee的方法SalarySort,如何解决这个问题?