Java 试图在类构造函数中将两个BigInteger数相加并获取stackoverflow错误
试图在类构造函数中将两个大整数相加并获取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
导入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中添加任何内容