【发布时间】:2014-08-15 10:58:22
【问题描述】:
你好,我对 java 还是有点陌生。当涉及到实例变量时,我得到了“this”的概念,但是当我在没有参数的构造函数中使用它时,我有点困惑。所以我的问题是这样的事情是如何工作的?
private double x;
private double y;
public static final double EPSILON = 1e-5;
public static boolean debug = false;
public Point(double x, double y){
this.x=x;
this.y=y; // Done sets the x,y private types to the x,y type provided in the ()
}
public Point(){
this(0.0,0.0); //Sets, x and y to doubles of 0.0,0.0??
} //How does this work?
我的 point() 构造函数会通过调用点 (x,y) 构造函数来创建 (0.0,0.0) 的原点吗?对此的任何澄清都会对我有很大帮助!
【问题讨论】:
-
“我的 point() 构造函数会通过调用点 (x,y) 构造函数来创建 (0.0,0.0) 的原点吗?” - 是的,这就是重点。
this(...)允许您将构造函数调用链接在一起以确保对象在创建时的状态