此博客是本人从学生时代开始做笔记所用, 部分是工作所遇问题,做填坑笔记,部分闲来查阅资料,加上自己的理解所总结的学习笔记, 常忙得不可开交,若漏了资料来源,望通知~ 前路漫漫,写点东西告诉自己正在一点点进步,而不要迷失于繁忙。

this()必须放在构造方法的第一条

public class A
{
    String name;
    int age;
    public A()
    {
        this("Jack",23);
    }
    public A(String name,int age)
    {
        this.name =name;
        this.age=age;
    }public static void main(String args[])
    {
        A test=new A();
        System.out.println("name is "+test.name+"I am "+test.age;
    }
    
}

在构造函数中,如果不在第一行指定构造器之间的调用关系,即使用this(),那么编译器会给你加上super()

那么就会发生super()->this()->super()的执行过程

为了避免多次创建对象,this()和super()的调用必须在第一行

 

posted @ 2018-11-27 16:29  炎泽  阅读(1732)  评论(0编辑  收藏  举报