Call to ‘super()‘ must be first statement in constructor body

简介: 文章解释了在Java中子类构造函数中调用父类构造函数时,必须首先调用`super()`,且不能有返回值。

原因是在子类继承父类构造函数的时候,子类构造函数使用void修饰返回值了。

在这里插入代码片

在这里插入图片描述
正确的应该去掉void:

package Student;

import myutils.*;

public class Student extends Common {
   

    public Student() {
   
    }

    public  Student(String name, int age, int money) {
   
        super(name, age, money);
    }

    public void getSomeMoney(int money) {
   
        int hasMoney = super.getMoney();
        super.setMoney(hasMoney + money);
    }
}
目录
相关文章
|
1月前
Flutter-解决Try catch出现异常:type ‘_TypeError‘ is not a subtype of type ‘Exception‘ in type cast
Flutter-解决Try catch出现异常:type ‘_TypeError‘ is not a subtype of type ‘Exception‘ in type cast
49 1
ambiguous call to overloaded function
ambiguous call to overloaded function
error : Class declarations lacks Q_OBJECT macro
error : Class declarations lacks Q_OBJECT macro
Dart报The return type ‘bool‘ isn‘t a ‘void‘, as required by the closure‘s context
Dart报The return type ‘bool‘ isn‘t a ‘void‘, as required by the closure‘s context
|
PyTorch 算法框架/工具
torch中报错:AttributeError: 'builtin_function_or_method' object has no attribute 'detach'怎么解决?
这个错误信息 "AttributeError: 'builtin_function_or_method' object has no attribute 'detach'" 表示你尝试在一个内置函数或方法对象上调用 detach() 方法,而这种对象没有这个属性。 detach() 是 PyTorch 张量和变量的方法,允许它们从计算图中分离出来,因此不能在其他类型的对象上调用。要解决这个错误,请确保你正在一个 PyTorch 张量或变量上调用 detach() 方法。
1020 0
|
JavaScript
Object.prototype.toString.call()的原理
Object.prototype.toString.call()的原理
Object.prototype.toString.call()的原理
|
Java Android开发
The method call() of type XXX must override a superclass
The method call() of type XXX must override a superclass
97 0
单方法对象(Single-method Object)
单方法对象(Single-method Object)
97 2
【错误记录】Groovy 注入方法报错 ( Cannot add new method [hello] for arguments [[]]. It already exists )
【错误记录】Groovy 注入方法报错 ( Cannot add new method [hello] for arguments [[]]. It already exists )
146 0
【错误记录】Groovy 注入方法报错 ( Cannot add new method [hello] for arguments [[]]. It already exists )
|
Android开发 Kotlin
【错误记录】Kotlin 编译报错 ( Not nullable value required to call an ‘iterator()‘ method on for-loop range )
【错误记录】Kotlin 编译报错 ( Not nullable value required to call an ‘iterator()‘ method on for-loop range )
260 0
【错误记录】Kotlin 编译报错 ( Not nullable value required to call an ‘iterator()‘ method on for-loop range )