课本上的例子
package practice;
public class P16 {
int x = 188,y;
public static void main(String[] args) {
P16 e= new P16();
e.f();
}
void f(){
int x=3;
y=x;//y得到的值是3,而非成员变量x的值(188)
System.out.println("y="+y);
y=this.x;//y得到的值是成员变量x的值,即:188
System.out.println("y="+y);
}
}
我自己的
package practice;
public class A {
public static void main(String[] args) {
int x=1;
A b = new A();
b.f();
}
void f() {
int y;
y = this.x;
System.out.println(y);
}
}
为什么y=this.x会报错呢?
------解决思路----------------------
x是main先的局部变量,不是成员变量,没法用this去引用
你也超出了它的可视范围(作用域)