Java 试图在类构造函数中将两个BigInteger数相加并获取stackoverflow错误

Java 试图在类构造函数中将两个BigInteger数相加并获取stackoverflow错误,java,Java,试图在类构造函数中将两个大整数相加并获取stackoverflow错误。 导入java.math.biginger public class BigNumber implements Cloneable { BigInteger sum = BigInteger.valueOf(0); BigInteger sum2 = BigInteger.valueOf(0); BigNumber(String g) { sum = sum.add(new Big

试图在类构造函数中将两个大整数相加并获取stackoverflow错误。
导入java.math.biginger

public class BigNumber implements Cloneable {

    BigInteger sum = BigInteger.valueOf(0);
    BigInteger sum2 = BigInteger.valueOf(0);

    BigNumber(String g) {
        sum = sum.add(new BigInteger(g));
    }

    public String ToString() {
        String a = "" + sum;
        return a;
    }

    public Object CloneNumber() throws CloneNotSupportedException {
        return super.clone();
    }

    public BigNumber add(BigNumber other) throws CloneNotSupportedException {
        return ((BigNumber) BigNumber.this.CloneNumber()).add(other);
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        BigNumber g = new BigNumber("46376937677490009712648124896970078050417018260538");
        BigNumber j = new BigNumber("37107287533902102798797998220837590246510135740250").add(g);
        String f = j.ToString();
        System.out.print(f);
    }

}

堆栈溢出不在构造函数中。当你的
main
方法调用你的
BigNumber
上的
add

这是第一个问题:

public BigNumber add(BigNumber other) throws CloneNotSupportedException {
    return ((BigNumber) BigNumber.this.CloneNumber()).add(other); 
}
您正在调用
add
方法中的
add
。。。你认为这怎么行

不清楚为什么您同时拥有
sum
sum2
,但我希望您的
add
方法只需要:

public BigNumber add(BigNumber other) throws CloneNotSupportedException {
    return new BigNumber(sum.add(other.sum).toString());
}
。。。虽然您最好通过重载构造函数来接受一个
BigInteger
,此时您可以:

public BigNumber add(BigNumber other) throws CloneNotSupportedException {
    return new BigNumber(sum.add(other.sum));
}
this.sum = new BigInteger(g);
构造函数(和字段声明)将如下所示:

// Personally I'd make the class final, but that's a different matter
public class BigNumber {
    private final BigInteger value; // Renamed from sum - see below

    public BigNumber(BigInteger value) {
        this.value = value;
    }

    public BigNumber(String value) {
        this(new BigInteger(value));
    }

    ... methods ...
}
此外:

  • 您应该覆盖现有的
    toString
    方法,而不是引入自己的
    toString
    方法
  • 您应该重写
    clone()
    ,而不是引入自己的
    CloneNumber
    方法
  • 遵循Java命名约定,其中方法是camelCased而不是PASCALCASE
  • 您应该删除类中未使用的
    sum2
    字段
  • 您不需要在构造函数中添加任何内容,只需:

    public BigNumber add(BigNumber other) throws CloneNotSupportedException {
        return new BigNumber(sum.add(other.sum));
    }
    
    this.sum = new BigInteger(g);
    
  • 您的字段名为
    sum
    ,但它显然不是任何内容的总和,它只是值。我会将其重命名为
    value
    或类似的名称

  • 现在还不清楚你为什么要这样做-你只是复制了
    biginger
    类的一小部分。。。为什么?

堆栈溢出不在构造函数中。当你的
main
方法调用你的
BigNumber
上的
add

这是第一个问题:

public BigNumber add(BigNumber other) throws CloneNotSupportedException {
    return ((BigNumber) BigNumber.this.CloneNumber()).add(other); 
}
您正在调用
add
方法中的
add
。。。你认为这怎么行

不清楚为什么您同时拥有
sum
sum2
,但我希望您的
add
方法只需要:

public BigNumber add(BigNumber other) throws CloneNotSupportedException {
    return new BigNumber(sum.add(other.sum).toString());
}
。。。虽然您最好通过重载构造函数来接受一个
BigInteger
,此时您可以:

public BigNumber add(BigNumber other) throws CloneNotSupportedException {
    return new BigNumber(sum.add(other.sum));
}
this.sum = new BigInteger(g);
构造函数(和字段声明)将如下所示:

// Personally I'd make the class final, but that's a different matter
public class BigNumber {
    private final BigInteger value; // Renamed from sum - see below

    public BigNumber(BigInteger value) {
        this.value = value;
    }

    public BigNumber(String value) {
        this(new BigInteger(value));
    }

    ... methods ...
}
此外:

  • 您应该覆盖现有的
    toString
    方法,而不是引入自己的
    toString
    方法
  • 您应该重写
    clone()
    ,而不是引入自己的
    CloneNumber
    方法
  • 遵循Java命名约定,其中方法是camelCased而不是PASCALCASE
  • 您应该删除类中未使用的
    sum2
    字段
  • 您不需要在构造函数中添加任何内容,只需:

    public BigNumber add(BigNumber other) throws CloneNotSupportedException {
        return new BigNumber(sum.add(other.sum));
    }
    
    this.sum = new BigInteger(g);
    
  • 您的字段名为
    sum
    ,但它显然不是任何内容的总和,它只是值。我会将其重命名为
    value
    或类似的名称

  • 现在还不清楚你为什么要这样做-你只是复制了
    biginger
    类的一小部分。。。为什么?

堆栈溢出不在构造函数中。当你的
main
方法调用你的
BigNumber
上的
add

这是第一个问题:

public BigNumber add(BigNumber other) throws CloneNotSupportedException {
    return ((BigNumber) BigNumber.this.CloneNumber()).add(other); 
}
您正在调用
add
方法中的
add
。。。你认为这怎么行

不清楚为什么您同时拥有
sum
sum2
,但我希望您的
add
方法只需要:

public BigNumber add(BigNumber other) throws CloneNotSupportedException {
    return new BigNumber(sum.add(other.sum).toString());
}
。。。虽然您最好通过重载构造函数来接受一个
BigInteger
,此时您可以:

public BigNumber add(BigNumber other) throws CloneNotSupportedException {
    return new BigNumber(sum.add(other.sum));
}
this.sum = new BigInteger(g);
构造函数(和字段声明)将如下所示:

// Personally I'd make the class final, but that's a different matter
public class BigNumber {
    private final BigInteger value; // Renamed from sum - see below

    public BigNumber(BigInteger value) {
        this.value = value;
    }

    public BigNumber(String value) {
        this(new BigInteger(value));
    }

    ... methods ...
}
此外:

  • 您应该覆盖现有的
    toString
    方法,而不是引入自己的
    toString
    方法
  • 您应该重写
    clone()
    ,而不是引入自己的
    CloneNumber
    方法
  • 遵循Java命名约定,其中方法是camelCased而不是PASCALCASE
  • 您应该删除类中未使用的
    sum2
    字段
  • 您不需要在构造函数中添加任何内容,只需:

    public BigNumber add(BigNumber other) throws CloneNotSupportedException {
        return new BigNumber(sum.add(other.sum));
    }
    
    this.sum = new BigInteger(g);
    
  • 您的字段名为
    sum
    ,但它显然不是任何内容的总和,它只是值。我会将其重命名为
    value
    或类似的名称

  • 现在还不清楚你为什么要这样做-你只是复制了
    biginger
    类的一小部分。。。为什么?

堆栈溢出不在构造函数中。当你的
main
方法调用你的
BigNumber
上的
add

这是第一个问题:

public BigNumber add(BigNumber other) throws CloneNotSupportedException {
    return ((BigNumber) BigNumber.this.CloneNumber()).add(other); 
}
您正在调用
add
方法中的
add
。。。你认为这怎么行

不清楚为什么您同时拥有
sum
sum2
,但我希望您的
add
方法只需要:

public BigNumber add(BigNumber other) throws CloneNotSupportedException {
    return new BigNumber(sum.add(other.sum).toString());
}
。。。虽然您最好通过重载构造函数来接受一个
BigInteger
,此时您可以:

public BigNumber add(BigNumber other) throws CloneNotSupportedException {
    return new BigNumber(sum.add(other.sum));
}
this.sum = new BigInteger(g);
构造函数(和字段声明)将如下所示:

// Personally I'd make the class final, but that's a different matter
public class BigNumber {
    private final BigInteger value; // Renamed from sum - see below

    public BigNumber(BigInteger value) {
        this.value = value;
    }

    public BigNumber(String value) {
        this(new BigInteger(value));
    }

    ... methods ...
}
此外:

  • 您应该覆盖现有的
    toString
    方法,而不是引入自己的
    toString
    方法
  • 您应该重写
    clone()
    ,而不是引入自己的
    CloneNumber
    方法
  • 遵循Java命名约定,其中方法是camelCased而不是PASCALCASE
  • 您应该删除类中未使用的
    sum2
    字段
  • 您不需要在y中添加任何内容