【问题标题】:Default Constructor in the Subclass Java子类 Java 中的默认构造函数
【发布时间】:2018-06-13 10:16:45
【问题描述】:

我正在创建一个扩展 BigInteger 类的类ExtendBigInt,并且需要能够将默认值设置为 0。但是,由于无法编辑 BigInteger,如何在子类中创建无参数构造函数?当我尝试创建没有参数的子类的构造函数时,它会抛出此错误Implicit super constructor BigInteger() is undefined. Must explicitly invoke another constructor

import java.math.BigInteger;

public class ExtendBigInt extends BigInteger{


    public ExtendBigInt() {

    }



    public static void main(String[] args) {
         //BigInteger one = new BigInteger("0");
         //BigInteger two = new BigInteger("1111111111111111111111");
         //BigInteger result = one.subtract(two);
         //System.out.println(one); // -1111111111110987654322
         //double down = result.doubleValue(); // returns a double 
         //System.out.println(down); // -1.1111111111109876E21
         //System.out.println(one.toString()); // 123456789
         //System.out.println(result.subtract(two)); // -2222222222222098765433
         //System.out.println(one.multiply(two)); // 137174209999999999999986282579
         //System.out.println(result.multiply(result).multiply(result).multiply(result));
         // 1524157902758048400486511315461168814591948287890638869506006559674928884826743139856
         //System.out.println(BigInteger.ONE); // Commonly used BigInteger("1") 

    }

}

【问题讨论】:

  • 你在构造函数中试过super(0);吗?
  • 我刚刚试了一下,效果很好!

标签: java inheritance constructor extends


【解决方案1】:

做事

public ExtendBigInt() {
    super("0");
}

BigInteger 中没有空构造函数,因此您会收到该错误

【讨论】:

  • 非常感谢。我不知道为什么我在实际尝试时无法理解这个概念!
【解决方案2】:

正如@ShashwatKanna 已经指出的那样,您的问题的字面答案是调用super(0)

但也许另一种解决方案可能会更好地解决您的问题:不要扩展 BigInteger

从您的示例中,我看不出您的班级应该扩展BigInteger 的原因。您可以在任何您喜欢的类中使用BigInteger 算术,而无需扩展BigInteger,例如:

import java.math.BigInteger;

public class ExtendBigInt {

    public static void main(String[] args) {
         BigInteger one = new BigInteger("0");
         BigInteger two = new BigInteger("1111111111111111111111");
         BigInteger result = one.subtract(two);
         System.out.println(one);
         double down = result.doubleValue();
         System.out.println(down);
         System.out.println(one.toString());
         System.out.println(result.subtract(two));-2222222222222098765433
         System.out.println(one.multiply(two));
         System.out.println(result.multiply(result).multiply(result).multiply(result));

         System.out.println(BigInteger.ONE);
    }
}

public ExtendBigInt() { ... } 构造函数没有用处,因为您的代码中没有 new ExtendBigInt(...) 调用。

如果你真的想扩展BigInteger,那么我看不到只有无参数构造函数的有效用例:

  • 扩展BigInteger 意味着创建具有一些附加属性(字段、方法、行为...)的无限大小的整数对象(BigIntegers)。 no-args 构造函数只能创建一个常量值,可能是 0,以后不能更改,所以它只会创建一个“ExtendBigZero”。
  • 您至少应该添加一个public ExtendBigInt(String digits) { ... } 构造函数或类似的东西,以允许您创建值不为零的实例。
  • 你的类中应该有一些额外的字段或方法(不是静态的),所以ExtendBigIntBigInteger 确实不同 - 如果ExtendBigInt 的行为与BigInteger 完全相同,为什么有不同的班级吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-08
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    相关资源
    最近更新 更多