美文网首页
17-构造代码块

17-构造代码块

作者: c88bc9f9d088 | 来源:发表于2020-10-27 11:09 被阅读0次

    构造块是定义在一个类之中的。
范例:观察构造块

class Person{
    public Person(){
        System.out.println("【构造方法】Person类构造方法执行");
    }
    {
        System.out.println("【构造块】Person构造块执行");
    }
}
public class JavaDemo{
    public static void main(String args[]){
        new Person();
        new Person();
        new Person();
    }   
}

    构造块会优先于构造方法执行,并且每一次实例化新对象的时候都会调用构造块中的代码。

相关文章

网友评论

      本文标题:17-构造代码块

      本文链接:https://www.haomeiwen.com/subject/vmntvktx.html