【问题标题】:How Do I Fix My Super Statements?如何修复我的超级语句?
【发布时间】:2014-01-08 03:14:12
【问题描述】:

我以正确的格式放置了我的超级语句,但我唯一的错误是它指出类 java.lang.object 中的构造函数对象不能应用于这部分代码的给定类型:

       super (openFile (filename)); 

这是我现在的代码:

     import    java.io.BufferedReader;
     import java.io.ByteArrayOutputStream;
     import java.io.File;
     import java.io.FileNotFoundException;
     import java.io.FileReader;
     import java.io.IOException;
     import java.io.OutputStream;
     import java.io.OutputStreamWriter;
         import java.security.MessageDigest;
         import java.security.NoSuchAlgorithmException;
         import java.util.Vector;import java.io.PrintWriter;
         import java.io.BufferedWriter;
         import java.io.FileWriter;
         import java.io.InputStream;
         import java.io.InputStreamReader;
         import java.io.Reader;

       public class Buffin 
      {             
 private static boolean temp;
 /////////////////////////////////
 private boolean isKeyboard;


 /** Connect to the disk file with the given name.  If this
  *  cannot be done, connect to the keyboard instead. */

 public Buffin (String filename)
 {    super (openFile (filename));
      isKeyboard = temp;
 }    //======================


 private static Reader openFile (String filename)
 {    try
      {    temp = false;
           return new FileReader (filename);  // IOException here
      }catch (IOException e)
      {    temp = true;
           return new InputStreamReader (System.in);
      }
 }    //======================


 /** Read one line from the file and return it.  
  *  Return null if at the end of the file. */

 public String readLine()
 {    if (isKeyboard)
      {    System.out.print (" input> ");
           System.out.flush();  // flush the output buffer
      }
      try
      {    return super.readLine(); // in BufferedReader
      }catch (IOException e)
      {    System.out.println ("Cannot read from the file");
           return null;
      }
 }    //============
}[/code]

【问题讨论】:

  • 没有接受ReaderObject构造函数。
  • 请按照编译器的说明进行操作!由于Reader 没有(Object) 的构造函数

标签: java super bluej


【解决方案1】:

您的类仅扩展了Object 类,它没有名为openFile 的方法或构造函数。如果您完全删除 super 电话,您会没事的。

【讨论】:

    【解决方案2】:

    目前,您的类Buffin 没有指定超类;这意味着它有一个超类,那就是java.lang.Object。在java.lang.Object 中没有采用Reader 的构造函数。

    您可以将Reader 作为一个字段存储在您的Buffin 类中,或者您可以扩展一个在构造函数中接受一个的超类。

    【讨论】:

      【解决方案3】:

      Super涉及到超类的构造函数,我看到Class Buffin没有继承任何类。正如@Sotirios Delimanolis 所说,在 Object 类中没有构造函数接受 Reader。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-21
        • 2020-11-06
        • 1970-01-01
        • 2015-01-26
        • 2019-02-26
        • 2021-01-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多