简体   繁体   English

super()不在子类'构造函数中工作

[英]super() not working in subclass' constructor

Superclass: 超类:

public class CwDB{
protected LinkedList<Entry> dict = null;

public CwDB(String filename){
        this.dict = new LinkedList<Entry>();
        try{
            FileReader fr = new FileReader(filename);
            BufferedReader br = new BufferedReader(fr);
            String w = null;
            while((w = br.readLine()) != null ){
                String c = br.readLine();
                this.add(w,c); //adds new Entry to dict
            }
            br.close();
            fr.close();
        }catch(IOException e){
            e.printStackTrace();
        }
}
public void add(String word, String clue){
    this.dict.add(new Entry(word,clue));
}
...
}

Subclass: 子类:

public class InteliCwDB extends CwDB {

public InteliCwDB(String filename){
    super(filename);
}
}

Case 1: 情况1:

CwDB db = new CwDB("src/cwdb.txt");

Case 2: 案例2:

InteliCwDB idb = new InteliCwDB("src/cwdb.txt");

The problem is that case 1 works perfectly, but case 2 doesn't at all. 问题是案例1完美无缺,但案例2根本没有。 Can you tell me what's wrong? 你能告诉我什么是错的吗? (I don't get any error/exception, just the idb's list is empty, when db's list has ober +1k elements...) (我没有得到任何错误/异常,只是idb的列表是空的,当db的列表有ober + 1k元素...)

You say that: 你这么说:

  1. There are no exceptions / stack traces. 没有异常/堆栈跟踪。
  2. The dict list in the created InteliCwDB instance is empty. 创建的InteliCwDB实例中的dict列表为空。

If the superclass constructor somehow didn't run, you would have an instance with a dict that was null . 如果超类构造函数以某种方式没有运行,那么你将拥有一个dictnull的实例。

If some other exception was thrown and not caught, then you wouldn't have a InteliCwDB to examine. 如果抛出了一些其他异常并且未被捕获,那么您将无法检查InteliCwDB

So on the face of it, this can only mean that the constructor is running , but the file it reads is empty. 所以从表面上看,这只能意味着构造函数正在运行 ,但它读取的文件是空的。 In other words you have two different "src/cwdb.txt" files ... in different directories. 换句话说,你有两个不同的“src / cwdb.txt”文件......在不同的目录中。


Other explanations involve questioning your evidence; 其他解释涉及询问您的证据; eg 例如

  • You are describing the symptoms inaccurately. 您正在准确地描述症状。
  • The code you are actually running is substantially different to what you are showing us; 您实际运行的代码与您向我们展示的代码大不相同; eg there is some other code that is leaping in and emptying the dict after the constructor has created and filled it. 例如,在构造函数创建并填充它之后,还有一些其他代码正在跳跃并清空dict (You haven't declared dict to be private ...). (你还没有声明dict是私人的......)。
    [ UPDATE - this was the explanation: see the OP's comments. [ 更新 - 这是解释:见OP的评论。 He had overridden add in the subclass, and that's where the bug was. 他已经覆盖了子类中的add ,这就是bug的所在。 ] ]
  • The code you are running doesn't match your source code; 您运行的代码与您的源代码不符; ie you are not rebuilding / redeploying properly. 即你没有正确地重建/重新部署。

My advice would be to recheck your processes and your evidence. 我的建议是重新检查您的流程和证据。 And if that doesn't help, then run the application using a debugger, set a breakpoint on the constructor, and single step it so that you can work out what is really happening. 如果这没有帮助,那么使用调试器运行应用程序,在构造函数上设置断点,然后单步执行它以便您可以计算出真正发生的事情。

You should add a catch (Exception e) to your try-catch for the purposes of debugging. 您应该为try-catch添加一个catch (Exception e)以进行调试。 An exception that isn't an IOException might be causing you problems. 不是IOException的异常可能会导致问题。 So, try like this: 所以,试试这样:

public CwDB(String filename){
    this.dict = new LinkedList<Entry>();
    try{
        FileReader fr = new FileReader(filename);
        BufferedReader br = new BufferedReader(fr);
        String w = null;
        while((w = br.readLine()) != null ){
            String c = br.readLine();
            this.add(w,c); //adds new Entry to dict
        }
        br.close();
        fr.close();
    }catch(IOException e){
        e.printStackTrace();
    }catch(Exception e){
        e.printStackTrace();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM