问题
I am trying to write code for the following question : WAP to get empno ,DOB and salary of few employees. Write two anonymous inner class , one that implements an interface to sort the employees as per their DOB and another anonymous inner class that extends a class to sort the employees as per their salary . My code is :
//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) {
}
}
My question is,I cant call the anonymous class Employee's method SalarySort,how to solve the problem?
回答1:
If you trim down the code of the non-compiling part, it boils down to
Employee abc = new Employee() { ... }.SalarySort(list);
So, you're trying to initialize the variable abc
, of type Employee, with the value returned by the method SalarySort()
, which returns void.
You simply want
Employee abc = new Employee() { ... };
abc.SalarySort(list);
or
new Employee() { ... }.SalarySort(list);
BTW, reading the compiler error message would probably have helped finding the problem.
Another problem is that the above code should be inside the main method, where list
is defined, and not outside of it.
Also, respect the Java naming conventions, and choose good names for your classes, methods and variables. Don't name Employee something which is not en employee. Don't name XxxInnerClass something that is not an inner class.
回答2:
How about this one ? it uses Anonymous inner class which implements an interface and also extends a class.
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;
}
}
}
来源:https://stackoverflow.com/questions/18381094/call-anonymous-class-method-in-java