编译器无法正确识别构造函数覆盖

Sup*_*tar 2 java netbeans compiler-errors

我会让代码和错误进行讨论,因为我真的认为他们会说除了这一切之外的一切都不应该发生!有谁知道如何编译?

码


  class CountDownTimerGUI extends BHTimerGUI
  {
    private TimerJPanel control;
    private TimerDisplayJPanel disp;

    public CountDownTimerGUI(TimerJPanel control, TimerDisplayJPanel disp)
>>  {
      this.control = control;
      this.disp = disp;
    }
  }
Run Code Online (Sandbox Code Playgroud)

(>>表示错误行)

这会覆盖构造函数BHTimerGUI,其构造函数如下:

  public BHTimerGUI(TimerJPanel control, TimerDisplayJPanel disp)
  {
    this.control = control;
    this.disp = disp;
  }
Run Code Online (Sandbox Code Playgroud)

编译错误


I:\Java\NetBeansProjects\Blue Husky's Timer 2.0.0\src\bhtimer\GUI.java:145: cannot find symbol
symbol  : constructor BHTimerGUI()
location: class bhtimer.BHTimerGUI
    {
Run Code Online (Sandbox Code Playgroud)

NetBeans显示带有以下文本的弹出窗口:

constructor BHTimerGUI in class bhtimer.BHTimerGUI cannot be applied to given types;
  required: bhtimer.TimerJPanel,bhtimer.TimerDisplayJPanel
  found: no arguments
  reason: actual and formal argument lists differ in length
Run Code Online (Sandbox Code Playgroud)

Fed*_*era 6

是的,这应该发生!您没有初始化超类构造函数.尝试使用此构造函数:

   public CountDownTimerGUI(TimerJPanel control, TimerDisplayJPanel disp){
      super(control, disp);
      this.control = control;
      this.disp = disp;
   }
Run Code Online (Sandbox Code Playgroud)


pla*_*nes 5

构造函数在Java中不可覆盖.

我们需要看到BHTimerGUI的构造函数,但听起来它不是一个no-args构造函数,在这种情况下你需要显式编写一个超级调用它,并使用正确的参数,例如将其添加为第一行CountDownTimerGUI中的构造函数:

 super(control, disp);
Run Code Online (Sandbox Code Playgroud)

编译器此刻将插入:

super();
Run Code Online (Sandbox Code Playgroud)

但这不会在超类中找到构造函数.