请大家帮忙回复一下这段代码是this(3);调用的public Test(int a)的构造方法呢?
public class Test {
private int a;
public Test(){
this(3);
}
public Test(int a){
this.a = a;
}

public static void main(String[] args) {
Test t = new Test();
t.run(1.1, true);
}
public double run(double oil,boolean drive){
System.out.println("a===="+a);
return 0.0;
}
}

解决方案 »

  1.   

    你可以认为这是JAVA语法的一个特殊约定,
    必须满足如下条件才可以这样写:
    1. 只有构造方法中才能this调用构造方法,其他任何地方都不行。
    2. 如果在构造方法中调用了,必须写在第一行。
    3. 如果在构造方法中调用了,能且只能一次示例代码:
    [code=Jav]package silenceburn;class MyClass {
    public int i;
    public String s;

    MyClass() {
    this(10);
    //this("haha"); can't twice
    } MyClass(int i) {
    //this.i = i; can't place here
    this("haha");
    this.i  = i;
    } MyClass(String s) {
    this.s = s;
    }
    }public class ConstructorTester { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    MyClass m = new MyClass();
    System.out.println(m.s + m.i);
    }}
    [/code]