【问题标题】:Java ByteArrayInputStream Implicit super constructor is undefined. Must explicitly invoke another constructorJava ByteArrayInputStream 隐式超级构造函数未定义。必须显式调用另一个构造函数
【发布时间】:2013-03-03 19:41:25
【问题描述】:

各位,我是新手。我的目的是通过扩展它从 ByteArrayInputStream 获取 byte[] buf 变量, 在这个http://www.java2s.com/Open-Source/Android/android-core/platform-libcore/java/io/ByteArrayInputStream.java.htm 告诉 ByteArrayInputStream 没有无参数构造函数,但是当我编码时:

class Test extends ByteArrayInputStream {
    public Test(int i){}
}

eclipse 告诉我:隐式超级构造函数 ByteArrayInputStream() 未定义。必须显式调用另一个构造函数。 在问这个之前,我搜索了谷歌然后得到了这些:Java error: Implicit super constructor is undefined for default constructor,它告诉如果类 B 扩展类 A,那么类 A 必须定义一个无参数构造函数。好的,对于我们编写的类来说这很容易,但是 Sun 包中的类呢......我也想知道这个

提前致谢。

【问题讨论】:

    标签: java constructor explicit


    【解决方案1】:

    要记住的两个概念:

    1. 默认情况下,任何子类构造函数都会调用无参数构造函数 超级类。
    2. 即使在一个类中定义了一个构造函数,jvm 也不提供无参数构造函数。

    这里,public Test(int i) 将调用不存在的 ByteArrayInputStream()。所以你必须在 Test(int i) 的第一条语句中调用任何现有的 ByteArrayInputStream 构造函数,就像 super(required_pa​​rameters);

    【讨论】:

      【解决方案2】:

      您的 Test 类应该至少有两个构造函数:

      public class Test extends ByteArrayInputStream {
          public Test(byte[] buf) {
              super(buf);
          }
      
          public Test(byte[] buf, int offset, int length) {
              super(buf, offset, length);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-06-17
        • 2020-09-29
        • 2014-11-06
        • 2023-04-08
        • 1970-01-01
        • 2014-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多