import java.util.*;
public class Employee implements Comparable {
String name;
int age;
public Employee(String name,int age)
{
this.name=name;
this.age=age;
}
public String toString()
{
return "name"+this.name+"Age"+this.age;
} @Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
Employee temp=(Employee)o;
if(this.age==temp.age)
return 0;
else if(this.age>temp.age)
return 1;
else
return -1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee[] employees=new Employee[]{
new Employee("jack",21),
new Employee("tom",23),
};
Arrays.sort(employees);
for(int i=0;i<employees.length;i++)
{
System.out.println(employees[i].toString());
}
}
}
报错说:java.lang.NoSuchMethodError: main
Exception in thread "main" 拜托各位!
public class Employee implements Comparable {
String name;
int age;
public Employee(String name,int age)
{
this.name=name;
this.age=age;
}
public String toString()
{
return "name"+this.name+"Age"+this.age;
} @Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
Employee temp=(Employee)o;
if(this.age==temp.age)
return 0;
else if(this.age>temp.age)
return 1;
else
return -1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee[] employees=new Employee[]{
new Employee("jack",21),
new Employee("tom",23),
};
Arrays.sort(employees);
for(int i=0;i<employees.length;i++)
{
System.out.println(employees[i].toString());
}
}
}
报错说:java.lang.NoSuchMethodError: main
Exception in thread "main" 拜托各位!
import java.util.*;public class Employee implements Comparable {
String name;
int age; public Employee(String name, int age) {
this.name = name;
this.age = age;
} public String toString() {
return "name" + this.name + "Age" + this.age;
} public static void main(String[] args) {
// TODO Auto-generated method stub
Employee[] employees = new Employee[] { new Employee("jack", 21),
new Employee("tom", 23), };
Arrays.sort(employees); for (int i = 0; i < employees.length; i++) {
System.out.println(employees[i].toString());
}
}
public int compareTo(Object o) {
// TODO Auto-generated method stub
Employee temp = (Employee) o;
if (this.age == temp.age)
return 0;
else if (this.age > temp.age)
return 1;
else
return -1; }
}没问题,上面的代码在我机子上运行通过!只是去掉了compareTo方法上面的@Override
这个是一个接口 你可以查 JDK文档