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()的调用必须在第一行

 

相关文章:

  • 2021-04-11
  • 2021-03-02
  • 2021-06-29
  • 2022-12-23
  • 2022-12-23
  • 2021-04-25
  • 2022-12-23
  • 2022-02-13
猜你喜欢
  • 2022-12-23
  • 2021-10-06
  • 2022-12-23
  • 2021-08-21
  • 2022-12-23
  • 2021-07-12
相关资源
相似解决方案