简体   繁体   English

构造函数调用必须是 super() 构造函数中的第一条语句

[英]Constructor call must be the first statement in a constructor in super()

I'm following a guide on how to create a working interface in Java and I've done the code according to the guide yet I get an error saying Constructor call must be the first statement in a constructor , even though it is the first statement.我正在遵循有关如何在 Java 中创建工作接口的指南,我已经根据指南完成了代码,但我收到一条错误消息,说Constructor call must be the first statement in a constructor ,即使它是第一条语句. I tried multiple solutions so I'm quite lost as none of them worked.我尝试了多种解决方案,所以我很迷茫,因为它们都不起作用。

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class test extends JFrame {                  
    private JLabel item1;
    private void test () {
        super ("Title 1"); //error happens here     
      }
}

You didn't create a constructor - constructors don't have return type of void.您没有创建构造函数 - 构造函数没有 void 返回类型。 It should be它应该是

private test (){  
    super ("Title 1");  
} 

but you actually should stick to Java naming conventions and rename your class to Test .但您实际上应该坚持 Java 命名约定并将您的类重命名为Test

The constructor should not have a return value构造函数不应有返回值

private test () {
    super(); // This should do
}

You need to remove the void from your test function.您需要从test函数中删除void

Your code must be like:你的代码必须是这样的:

public test (){
    super ("Frame Title");
}

暂无
暂无

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

相关问题 对super()的调用必须是构造函数体中的第一个语句 - call to super() must be the first statement in constructor body 对 super 的调用必须是构造函数中的第一条语句,但它是 - Call to super must be first statement in the constructor, but it is 使用super()时,构造函数调用必须是构造函数中的第一条语句; - Constructor Call must be the first statement in a constructor while using super(); Zebra.java:3:错误:对super的调用必须是构造函数中的第一条语句 - Zebra.java:3: error: call to super must be first statement in constructor 继承的构造函数java:对super()的调用必须是第一条语句 - Inherited constructor java : Call to super() must be first statement 错误:超级必须是构造函数中的第一条语句 - error: Super must be first statement in constructor “构造函数调用必须是构造函数中的第一条语句”错误 - “Constructor call must be the first statement in a constructor” error 构造函数错误“对此的调用必须是构造函数中的第一条语句” - Constructor error “call to this must be first statement in constructor” 构造函数调用必须是第一个语句…错误,但不是构造函数 - Constructor call must be first statement…error, but it is NOT a constructor 构造函数调用必须是具有继承关系的构造函数中的第一条语句 - Constructor call must be the first statement in a constructor with inheritance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM