this或super调用构造器

public class test4 {
    public test4(int a,int b,int c){
        this(a,b);
        //this(a);   //此行报错,意味着在一个构造器里面只能调用一次构造器。
    }

    public test4(int a,int b) {
        this(a);
    }

    public test4(int a){
        System.out.println("调用成功!");
    }

    public test4(){
        int a;
        //this(a);     //此行报错,且提示:构造器的调用必须在一个构造器的第一行
    }

    public void TestThis(){
        //this(1);    //此行报错,且提示:构造器的调用必须在一个构造器的第一行
    }

    public static void main(String[] args) {
        new test4(1, 2, 3); //调用成功,意味着虽然一个构造器中只能调用一次其它构造器,但却可以通过“一个调一个”的方式调用多个构造器。
    }

}

posted @ 2020-08-02 08:57  economies  阅读(57)  评论(0)    收藏  举报