【发布时间】:2012-12-09 00:16:14
【问题描述】:
是否有充分的理由使用阴影字段的参数?这两者有什么区别:
public class Point {
public int x = 0;
public int y = 0;
//constructor
public Point(int a, int b) {
x = a;
y = b;
}
}
和
public class Point {
public int x = 0;
public int y = 0;
//constructor
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
如果您使用 this 关键字而不使用此示例中隐藏字段的参数会怎样(我猜这只是不必要的):
public class Point {
public int x = 0;
public int y = 0;
//constructor
public Point(int a, int b) {
this.x = a;
this.y = b;
}
}
【问题讨论】:
标签: java parameters field shadow