当前位置: 代码迷 >> J2SE >> 为什么小弟我这个程序中的变量x与y不能显示
  详细解决方案

为什么小弟我这个程序中的变量x与y不能显示

热度:31   发布时间:2016-04-23 21:30:48.0
为什么我这个程序中的变量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
  相关解决方案