构造器的this()

this() 这个方法是用于在函数的构造器里,调用方法的另一个构造器,有种ntr的感觉

 

一个构造器里只能有一个this(),且只能在首行

public class person {
    public int age;
    public String name;

    public person() {
        System.out.println("先跑这里");
    }



    public person(int age, String name) {
        this(age);
        this.age = age;
        this.name = name;
    }

    public person(int age) {
        this();
        System.out.println("再跑这里");
        this.age = age;
    }
}

 

posted @ 2021-08-17 16:13  霸王龙168  阅读(187)  评论(0)    收藏  举报