问题 请见注释! 谢谢!!!package test.liuhang;
public class Aa { int x, y; //全局变量 x y
void output() { //output方法用来输出 x y 的值
System.out.println(x);
System.out.println(y);
}
void output (int x, int y) //这里的参数x y 为什么不能传给 全局变量的 x y
{ 是不是参数 x y 只在当前方法有效
x=x;
y=y;
} public static void main(String[] args) {
Point pt;
pt = new Point();
pt.output(5,5); //最后我想打印出来 5 5 但是打印出来的确是0 0 ?
pt.output(); 这两个 5 5 为什么不能赋值给全局变量的 x y ??
} }
public class Aa { int x, y; //全局变量 x y
void output() { //output方法用来输出 x y 的值
System.out.println(x);
System.out.println(y);
}
void output (int x, int y) //这里的参数x y 为什么不能传给 全局变量的 x y
{ 是不是参数 x y 只在当前方法有效
x=x;
y=y;
} public static void main(String[] args) {
Point pt;
pt = new Point();
pt.output(5,5); //最后我想打印出来 5 5 但是打印出来的确是0 0 ?
pt.output(); 这两个 5 5 为什么不能赋值给全局变量的 x y ??
} }
在该方法中会遮盖住全局变量,应该为
void output (int x, int y) //这里的参数x y 为什么不能传给 全局变量的 x y
{ 是不是参数 x y 只在当前方法有效 this.x = x;//
this.y = y;//}
{ x = x; //--等号右边这个x不就是 output 方法里边 的int x 吗?
y = y; //--等号右边这个y不就是 output 方法里边 的int y 吗?
--可是等号左边的 x 和 y 是什么? 是全局变量的 x y 吗?怎么运行的
呢?最后 的结果 是 0 5
}
System.out.println(x);
System.out.println(y);
} void output(int x, int y) { this.x = x;
this.y = y; } public static void main(String[] args) {
Aa aa = new Aa();
aa.output(5, 5);
aa.output(); }}