【问题标题】:Cannot figure out constructor error [duplicate]无法弄清楚构造函数错误[重复]
【发布时间】:2015-06-21 23:41:06
【问题描述】:

一段时间以来,我一直在努力完成以下任务。任务是创建一个程序,从输入文件中读取文本行,然后确定长度为 1 个字母、2 个字母等的单词的百分比。问题是我的类扩展了另一个类,我正在运行进入构造函数问题,虽然我没有看到。

我扩展的类:

public abstract class FileAccessor{
  String fileName; 
  Scanner scan;

  public FileAccessor(String f) throws IOException{
    fileName = f;
    scan = new Scanner(new FileReader(fileName));
  }

  public void processFile() { 
    while(scan.hasNext()){
      processLine(scan.nextLine());
    }
    scan.close();
  }

  protected abstract void processLine(String line);

  public void writeToFile(String data, String fileName) throws IOException{
        PrintWriter pw = new PrintWriter(fileName);
      pw.print(data);
      pw.close();
   }
}

我的班级:

import java.util.Scanner;
import java.io.*;

public class WordPercentages extends FileAccessor{
   public WordPercentages(String s){
     super.fileName = s;
     super.scan = new Scanner(new FileReader(fileName));
      }
   public void processLine(String file){
      super.fileName=file;
      int totalWords = 0;
      int[] length = new int[15];
      scan = new Scanner(new FileReader(super.fileName));
      while(super.scan.hasNext()){
         totalWords+=1;
         String s = scan.next();
         if (s.length() < 15){
            length[s.length()]+=1;
            }
         else if(s.length() >= 15){
            length[15]+=1;
            }
      }
   }

   public double[] getWordPercentages(){
      double[] percentages = new double[15];
      for(int j = 1; j < percentages.length; j++){
         percentages[j]+=length[j];
         percentages[j]=(percentages[j]/totalWords)*100;
         }
      return percentages; 
      }
   public double getAvgWordLength(){
      double average;
      for(int j = 1; j<percentages.length; j++){
         average+=(j*percentages[j])/totalWords;
         }
      return average;
      }
}

最后是我在运行驱动程序类时遇到的错误:

WordPercentages.java:8: error: constructor FileAccessor in class FileAccessor cannot be applied to given types;
   public WordPercentages(String s) {
                                   ^
  required: String
  found: no arguments
  reason: actual and formal argument lists differ in length

【问题讨论】:

    标签: java inheritance constructor java.util.scanner extends


    【解决方案1】:

    当你扩展另一个类时,子类构造函数中的第一条语句必须是对超类构造函数的调用。如果您不明确地这样做,它将隐式调用super()。在您的情况下,超级构造函数需要一个 String,但未提供,因此出现错误。

    那么你在哪里做:

    public WordPercentages(String s){
       super.fileName = s;
       super.scan = new Scanner(new FileReader(fileName));
    }
    

    你应该这样做:

    public WordPercentages(String s){
       super(s);
    }
    

    【讨论】:

      【解决方案2】:

      您没有显式调用WordPercentages 中的超类构造函数,因此Java 插入了对FileAccessor 中默认构造函数的隐式调用。对象的超类部分也必须构造。但是,FileAccessor 中没有这样的无参数构造函数。

      您正试图在子类构造函数中初始化对象的超类部分。相反,让超类构造函数来处理它。

      public WordPercentages(String s){
          super(s); 
      }
      

      您仍然必须捕获超类构造函数抛出的IOException(或声明它throws IOException 的子类构造函数)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-11
        • 1970-01-01
        • 2015-10-26
        • 2012-08-23
        • 1970-01-01
        • 1970-01-01
        • 2016-05-04
        相关资源
        最近更新 更多