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

为什么我这个程序中的变量x与y不能显示?
如题,为什么不能像变量salary一样输出?

public class Study
{
public static void main(String[] args)
{
Employee a = new Employee(50, 60);
  System.out.println(a.salary);
  System.out.println(a.x);
  System.out.println(a.y);

}
}

class Employee
{
double salary;
public Employee(double x, double y)
{

salary = 30;
}
}


会有这样的提示:



------解决方案--------------------
x,y是方法上的,只有你调用方法的时候才有,salary是全局变量,你可以点出来。
------解决方案--------------------

class Employee
{
        double salary;
        double x, y;
        public Employee(double x, double y)
        {
             this.x = x;
             this.y=y;
                    salary = 30;
        }
}

要先声明出来x,和y