如果我写以下课程:
public class Example {
int j;
int k;
public Example(int j, int k) {
j = j;
k = k;
}
public static void main(String[] args) {
Example exm = new Example(1,2);
System.out.println(exm.j);
System.out.println(exm.k);
}
}
程序可以编译,但是当我运行程序时,main方法会打印出两个0。我知道,为了说我想在构造函数中初始化实例变量,我必须写:
this.j = j;
this.k = k;
但是如果我不写它,那么在构造函数中(在表达式的左侧和写手侧)评估(或考虑)哪个变量?是参数还是实例变量?这有什么不同吗?
是否还有其他this
必须使用的情况?