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);
    }
}
目录
相关文章
|
PHP
Fatal error: Can't use method return value in write context in
说明:   php环境测试会报错下面错误:Fatal error: Can't use method return value in write context in    解决方案:   首先检查自己的php版本,支付宝sdk和demo测试需要php5.5以上版本。
960 12
|
7月前
Could not find method debug()
Could not find method debug()
260 59
|
PHP
set_error_handler()
set_error_handler()
56 0
|
JSON 数据格式
成功解决 global init errTypeError:Right-hand side of 'instanceof' is not callable
成功解决 global init errTypeError:Right-hand side of 'instanceof' is not callable
|
SQL Java 数据库连接
attempted to return null from a method with a primitive return type
attempted to return null from a method with a primitive return type
222 0
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
|
JavaScript
Object.prototype.toString.call()的原理
Object.prototype.toString.call()的原理
Object.prototype.toString.call()的原理
单方法对象(Single-method Object)
单方法对象(Single-method Object)
110 2
|
Java Android开发
The method call() of type XXX must override a superclass
The method call() of type XXX must override a superclass
112 0