如何防止错误的递归构造函数调用
How to prevent error recursive constructor invocation
我找不到解决方案。我正在使用 Java 11 和 IntelliJ IDEA。
错误在第 10 行,只有删除它才能消除错误,但我需要这个构造函数。
第一次遇到这个错误
public class Time2 {
private int seconds;
public Time2()
{
this(0);
}
public Time2(int hour)
{
this(hour*3600);
}
public Time2(int hour, int minute )
{
this(hour * 3600 + minute*60);
}
public Time2(int hour, int minute, int second)
{
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("hour must be 0-23");
if (minute < 0 || minute >= 60)
throw new IllegalArgumentException("minute must be 0-59");
if (second < 0 || second >= 60)
throw new IllegalArgumentException("second must be 0-59");
this.seconds = hour*3600 + minute*60 + second;
}
public Time2(Time2 time)
{
this(time.getHour(), time.getMinute(), time.getSecond());
}
}
构造函数之间存在递归调用:
public Time2() // A
{
this(0);
}
public Time2(int hour) // B
{
this(hour*3600);
}
public Time2(int hour, int minute ) // C
{
this(hour * 3600 + minute*60);
}
public Time2(int hour, int minute, int second) // D
{
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("hour must be 0-23");
if (minute < 0 || minute >= 60)
throw new IllegalArgumentException("minute must be 0-59");
if (second < 0 || second >= 60)
throw new IllegalArgumentException("second must be 0-59");
this.seconds = hour*3600 + minute*60 + second;
}
public Time2(Time2 time) // E
{
this(time.getHour(), time.getMinute(), time.getSecond());
}
- A -> B
- B -> B
- C -> B
- D -> -
- E -> D
所以对于构造函数A、B、C,它们都调用了B,然后就发生了无休止的递归调用。
您必须更正 C 和 B 构造函数:
public Time2(int hour, int minute ) // C
{
this(hour, minute, 0);
}
public Time2(int hour) // B
{
this(hour,0);
}
这样就避免了无休止的递归。
- A -> B
- B -> C
- C -> D
- D -> -
- E -> D
也只有在最后一个 (D) 构造函数中才会转换为秒。
PS:您应该使用 Java 已经存在的 API 转换为秒(例如:Duration.ofHours(1).getSeconds()
)。
您的第二个构造函数如下所示:
public Time2(int hour) {
this(hour*3600);
}
请注意,它会调用自身(称为递归调用)。并且构造函数不能以这种方式调用自身。您可能想改为设置 seconds
字段:
public Time2(int hour) {
seconds = hour*3600;
}
我找不到解决方案。我正在使用 Java 11 和 IntelliJ IDEA。 错误在第 10 行,只有删除它才能消除错误,但我需要这个构造函数。
第一次遇到这个错误
public class Time2 {
private int seconds;
public Time2()
{
this(0);
}
public Time2(int hour)
{
this(hour*3600);
}
public Time2(int hour, int minute )
{
this(hour * 3600 + minute*60);
}
public Time2(int hour, int minute, int second)
{
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("hour must be 0-23");
if (minute < 0 || minute >= 60)
throw new IllegalArgumentException("minute must be 0-59");
if (second < 0 || second >= 60)
throw new IllegalArgumentException("second must be 0-59");
this.seconds = hour*3600 + minute*60 + second;
}
public Time2(Time2 time)
{
this(time.getHour(), time.getMinute(), time.getSecond());
}
}
构造函数之间存在递归调用:
public Time2() // A
{
this(0);
}
public Time2(int hour) // B
{
this(hour*3600);
}
public Time2(int hour, int minute ) // C
{
this(hour * 3600 + minute*60);
}
public Time2(int hour, int minute, int second) // D
{
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("hour must be 0-23");
if (minute < 0 || minute >= 60)
throw new IllegalArgumentException("minute must be 0-59");
if (second < 0 || second >= 60)
throw new IllegalArgumentException("second must be 0-59");
this.seconds = hour*3600 + minute*60 + second;
}
public Time2(Time2 time) // E
{
this(time.getHour(), time.getMinute(), time.getSecond());
}
- A -> B
- B -> B
- C -> B
- D -> -
- E -> D
所以对于构造函数A、B、C,它们都调用了B,然后就发生了无休止的递归调用。
您必须更正 C 和 B 构造函数:
public Time2(int hour, int minute ) // C
{
this(hour, minute, 0);
}
public Time2(int hour) // B
{
this(hour,0);
}
这样就避免了无休止的递归。
- A -> B
- B -> C
- C -> D
- D -> -
- E -> D
也只有在最后一个 (D) 构造函数中才会转换为秒。
PS:您应该使用 Java 已经存在的 API 转换为秒(例如:Duration.ofHours(1).getSeconds()
)。
您的第二个构造函数如下所示:
public Time2(int hour) {
this(hour*3600);
}
请注意,它会调用自身(称为递归调用)。并且构造函数不能以这种方式调用自身。您可能想改为设置 seconds
字段:
public Time2(int hour) {
seconds = hour*3600;
}