Recursive Constructor Invocation

前端 未结 3 1643
执笔经年
执笔经年 2020-12-11 10:16
public class LecturerInfo extends StaffInfo {

    private float salary;

    public LecturerInfo()
    {
        this();
        this.Name = null;
        this.Addr         


        
相关标签:
3条回答
  • 2020-12-11 10:57

    the code below is recursive. Since this() will call no arg constructor of current class that means LectureInfo() again.

    public LecturerInfo()
    {
        this(); //here it translates to LectureInfo() 
        this.Name = null;
        this.Address = null;
        this.salary=(float) 0.0;
    }
    
    0 讨论(0)
  • 2020-12-11 11:04

    if you modify the fist constructor to this:

     public LecturerInfo()
     {
       this(null, null, (float)0.0);
     }
    

    this will be recursive.

    0 讨论(0)
  • 2020-12-11 11:08

    by calling this() you are calling your own constructor. By observing your code it seems you were supposed to call super() instead of this();

    0 讨论(0)
提交回复
热议问题