5.17 构造代码块

class Person {
    public Person(){
        System.out.println("[构造方法]Person 类构造方法执行");
    }
    {// 构造块会先于构造方法执行;并且每次实例化新对象的时候,都会调用构造块中的代码;
        System.out.println("[构造块] Person 构造块执行!");
    }
}

public class HelloWorld {
    public static void main(String[] args) {
        new Person();
        new Person();
        new Person();
    }
}

posted @ 2023-06-02 16:54  盘思动  阅读(11)  评论(0)    收藏  举报