递归构造函数调用 [英] Recursive Constructor Invocation

查看:328
本文介绍了递归构造函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class LecturerInfo extends StaffInfo {

    private float salary;

    public LecturerInfo()
    {
        this();
        this.Name = null;
        this.Address = null;
        this.salary=(float) 0.0;
    }

    public LecturerInfo(String nama, String alamat, float gaji)
    {
        super(nama, alamat);
        Name = nama;
        Address = alamat;
        salary = gaji;
    }

    @Override
    public void displayInfo()
    {
         System.out.println("Name :" +Name);
         System.out.println("Address :" +Address);
         System.out.println("Salary :" +salary);
    }
}

此代码显示错误:


递归构造函数调用LecturerInfo()

Recursive constructor invocation LecturerInfo()

因为无参构造函数与具有参数的构造函数冲突。

Is it because of the no-argument constructor having conflicts with the constructor with parameters?

推荐答案

下面的代码是递归的。由于 this()将不再调用当前类的参数构造函数,意味着 LectureInfo()

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

public LecturerInfo()
{
    this(); //this is calling LectureInfo() 
    this.Name = null;
    this.Address = null;
    this.salary=(float) 0.0;
}

这篇关于递归构造函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆