日期:2014-05-20  浏览次数:20815 次

新人求助
照着JAVA核心技术打的代码

public   class   StaticText   {
public   static   void   main(String[]   args)
{
Employee[]   staff=new   Employee[3];
staff[0]=new   Employee( "yanshi ",10000);
staff[1]=new   Employee( "njx ",60000);
staff[2]=new   Employee( "su ",10000);
for(int   i   =   0;   i <staff.length;i++)  
{ staff[i].setId();
System.out.println( "name= "+staff[i].getName()+ ",salary= "+staff[i].getSalary()+ "ID "+staff[i].getId());
        }
int   n=Employee.getNextId();
System.out.print( "nextid "+n);
}

class   Employee
{
private   String   name;
private   int   id;
private   static     int   nextId=1;
private   double   salary;
      public   Employee(String   n,double   s   )
{
  name=n;
  salary=s;
  id=0;
 
}
public   String   getName()
{
return   name;
}
public   double   getSalary()
{
return   salary;
}
public   int   getId()
{
return   id;
}
public   void   setId()
{
id=nextId;
nextId++;
}
public   static   int   getNextId()
{
return   nextId;
}
public   static   void   main(String[]   args)
{
Employee   e=new   Employee( "wangxin ",10000);
System.out.println(e.getName()+ "   "+e.getSalary());
}
}
}


结果eclips报错.....修改了好错次都没办法解决

问一下   是否是两个main   运行方式错误
Exception   in   thread   "main "   java.lang.Error:   无法解析的编译问题:
没有任何类型   StaticText   的外层实例可访问。必须用类型   StaticText   的外层实例(例如,x.new   A(),其中   x   是   StaticText   的实例)来限定分配。
没有任何类型   StaticText   的外层实例可访问。必须用类型   StaticText   的外层实例(例如,x.new   A(),其中   x   是   StaticText   的实例)来限定分配。
没有任何类型   StaticText   的外层实例可访问。必须用类型   StaticText   的外层实例(例如,x.new   A(),其中   x   是   StaticText   的实例)来限定分配。

at   StaticText.main(StaticText.java:5)


------解决方案--------------------
代码块范围错了,现在可以了:

public class StaticText {
public static void main(String[] args) {
Employee[] staff = new Employee[3];
staff[0] = new Employee( "yanshi ", 10000);
staff[1] = new Employee( "njx ", 60000);
staff[2] = new Employee( "su ", 10000);
for (int i = 0; i < staff.length; i++) {
staff[i].setId();
System.out.println( "name= " + staff[i].getName() + ",salary= "
+ staff[i].getSalary() + "ID " + staff[i].getId());
}
int n = Employee.getNextId();
System.out.print( "nextid " + n);
}
}

class Employee {
private String name;

private int id;

private static int nextId = 1;

private double salary;

public Employee(String n, double s) {
name = n;
salary = s;
id = 0;

}

public String getName() {
return name;
}

public double getSalary() {
return salary;
}