Call to ‘super()‘ must be first statement in constructor body异常解决和原因分析
super(),在子类中调用父类构造函数必须将其放在子类构造函数的首行。(问题来源educoder面向对象super关键字的使用)
eg:
class A
{
A(int a){}
}
class B extends A
{
B(int a){
super(a);
}
}
执行super语句必须将其放在子类的构造函数的第一句!!!
package case3;
public class superTest {
public static void main(String[] args) {
// 实例化一个Student类的对象s,为Student对象s中的school赋值,打印输出信息
/********* begin *********/
Student s=new Student();
s.school="哈佛大学";
s.p();
/********* end *********/
}
}
class Person {
/********* begin *********/
String name;
int age;
public Person(String a,int b){
System.out.print("姓名:"+a+",年龄:"+b);
}
/********* end *********/
}
class Student extends Person {
/********* begin *********/
String school;
public Student(){
super("张三",18);
}
public void p(){
System.out.print(",学校:"+school);
}
/********* end *********/
}