我刚学了内部类 想用一下
我想实现一个多接口继承的类WORKERS`
然后用ARRAY中的SORT方法对其对象进行排序(按照工龄(Wyear)的大小来排)`
最后输出相应对象的NAME成员变量`
代码如下:
import java.util.Arrays;
public class MyTest1
{
public interface person
{
String getName();
String getSex();
}
class Workers implements person,Comparable
{
private String name;
private String sex;
private int Wyear;
public Workers(String strname,String strsex,int year)
{
name=strname;
sex=strsex;
Wyear=year;
}
public String getName()
{
return name;
}
public String getSex()
{
return sex;
}
public int getWyear()
{
return Wyear;
}
public int compareTo(Object otherObject)
{
Workers other=(Workers)otherObject;
int otherWyear=other.Wyear;
int thisWyear=this.Wyear;
if(thisWyear>otherWyear)
return 1;
else if(thisWyear==otherWyear)
return 0;
else
return -1;
}
}
public static void main(String[] args)
{
Workers[] staff=new Workers[3];
staff[0]=new Workers("jojo","man",10);
staff[1]=new Workers("jeff","man",5);
staff[2]=new Workers("lina","woman",7);
Arrays.sort(staff);
for(int i=0;i<staff.length;i++)
{
System.out.println(staff[i].name);
}
}
}
可是编译时候出现同样的错误`
错误如下:
Mytest1.java:49:无法从静态上下文中引用非静态变量this
staff[0]=new Workers("jojo","man",10);
剩下的两个对象的创建也都是同样的错误。。。。
测试了半天没能通过
真心求指点啊
|