递归构造函数调用错误无法找到解决方案

时间:2014-06-18 11:23:25

标签: java recursion constructor this invocation

我在四个公共金枪鱼部分得到了递归构造溢出调用错误(部分=可能是一个类或其他东西?)。它适用于本教程但不适合我,似乎无法看到

public class tuna {
    private int hour;
    private int minute;
    private int second;

    public tuna() {
        this(0,0,0); //default  
    }
    public tuna(int h){
        this(h,0,0);    //with hours input
    }
    public tuna(int h, int m){
        this(h,m,0);    //with hours and minutes
    }
    public tuna(int h, int m, int s){
        this(h,m,s);    //with hours, minutes and seconds
    }

2 个答案:

答案 0 :(得分:7)

你在这里做一个递归电话:

public tuna(int h, int m, int s){
    this(h,m,s);    //with hours, minutes and seconds
}

您应该在此构造函数中设置私有成员。它应该是这样的:

public tuna(int h, int m, int s){
    this.h = h;    //with hours, minutes and seconds
    this.m = m;
    this.s = s;
}

答案 1 :(得分:-2)

public tuna(int h, int m, int s){
    this(h,m,s);    //with hours, minutes and seconds
}

Insead of this(h,m,s);使用setTime(h,m,s);

应该是:

public class tuna {
    private int hour;
    private int minute;
    private int second;

    public tuna() {
        this(0,0,0); //default  
    }
    public tuna(int h){
        this(h,0,0);    //with hours input
    }
    public tuna(int h, int m){
        this(h,m,0);    //with hours and minutes
    }
    public tuna(int h, int m, int s){
        setTime(h,m,s);    //with hours, minutes and seconds
    }  
   // define setTime method below