1

我正在尝试为以下问题编写代码: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,如何解决这个问题?

4

2 回答 2

3

如果你修剪非编译部分的代码,它归结为

Employee abc = new Employee() { ... }.SalarySort(list);

因此,您尝试abc使用方法返回的值初始化 Employee 类型的变量,该方法SalarySort()返回 void。

你只是想要

Employee abc = new Employee() { ... };
abc.SalarySort(list);

或者

new Employee() { ... }.SalarySort(list);

顺便说一句,阅读编译器错误消息可能有助于发现问题。

另一个问题是上面的代码应该在list定义的main方法内部,而不是在它之外。

此外,请尊重 Java 命名约定,并为您的类、方法和变量选择好的名称。不要将非雇员的名字命名为雇员。不要将 XxxInnerClass 命名为不是内部类的东西。

于 2013-08-22T13:06:02.200 回答
1

这个怎么样 ?它使用实现接口并扩展类的匿名内部类。

import java.util.Arrays;
import java.util.Comparator;

public class AnonymousInner
{
int empNo;
double salary;
MyDate dateOfBirth;
public AnonymousInner(int empNo, double salary, MyDate dateOfBirth)
{
    this.empNo = empNo;
    this.salary = salary;
    this.dateOfBirth = dateOfBirth;
}

@Override
public String toString()
{
    return "\nEmployee = " + empNo + " Salary= " + salary + " DOB= " + dateOfBirth;
}
public static void main(String[] args)
{
    AnonymousInner[] a =
    {new AnonymousInner(1, 5000, new MyDate(15, 0, 1994)), new AnonymousInner(2, 4000,
            new MyDate(15, 1, 1993)), new AnonymousInner(3, 5000, new MyDate(15, 0, 1995)),
            new AnonymousInner(4, 6000, new MyDate(16, 0, 1994)), new AnonymousInner(5, 9000,
                    new MyDate(15, 1, 1994))};

    Arrays.sort(a, new Comparator<AnonymousInner>()  //This creates anonymous class that implements an interface
    {

        @Override
        public int compare(AnonymousInner o1, AnonymousInner o2)
        {

            return ((Double) o1.salary).compareTo(o2.salary);
        }
    });
    System.out.println("Sorting on Salary");
    for (AnonymousInner s : a)
    {
        System.out.println(s);
    }

    a = new sortDOB()
    {
    }.sort(a);  //this creates Anonymous class that extends a class

    System.out.println("\nSorting on DOB\n");
    for (AnonymousInner s : a)
    {
        System.out.println(s);
    }

}

}

class sortDOB
{

public AnonymousInner[] sort(AnonymousInner[] x)
{
    int size = x.length;
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size - 1; j++)
        {
            int k = j + 1;
            int compare = (x[j].dateOfBirth).compareTo(x[k].dateOfBirth);
            if (compare == 1)
            {
                AnonymousInner temp = x[j];
                x[j] = x[k];
                x[k] = temp;

            }
        }
    }
    return x;
}
}

class MyDate implements Comparable<MyDate>
{
int year, month, day;
public MyDate(int day, int month, int year)
{
    this.year = year;
    this.month = month;
    this.day = day;
}

public MyDate()
{
}

@Override
public String toString()
{
    return day + "/" + month + "/" + year;
}

@Override
public int compareTo(MyDate o2)
{
    if (year == o2.year)
    {
        if (month == o2.month)
        {
            if (day == o2.day)
            {
                return 0;
            }
            else if (day < o2.day)
            {
                return -1;
            }
            else
            {
                return 1;
            }

        }
        else if (month < o2.month)
        {
            return -1;
        }
        else
        {
            return 1;
        }
    }
    else if (year < o2.year)
    {
        return 1;
    }
    else
    {
        return -1;
    }

}
}
于 2015-07-14T18:48:54.703 回答