获取错误"默认构造函数未定义隐式超级构造函数Student()。必须定义一个显式的构造函数"

时间:2018-01-18 05:30:21

标签: java

package implementation;

public abstract class Student {
   String name; int standard;

   abstract void getPercentage();
   static void getTotalNoOfStudents() {}

   public Student() {}
   public Student(String name, int standard) {
     this.name=name; this.standard=standard;
   }
}

public class ScienceStudent extends Student {
   int ScienceMarks;
   public static int noOfStudents=0;

   public ScienceStudent(String name, int standard, int ScienceMarks) {
       super(name, standard);
       this.ScienceMarks=ScienceMarks;
   }

   public void getPercentage() {
     System.out.println(10000/ScienceMarks);
   }
}

public class HistoryStudent extends Student {
   int historyMarks;
   public static int noOfStudents;

   public HistoryStudent(String name, int standard, int historyMarks) {
      super(name, standard);
      this.historyMarks=historyMarks;
   }

   public void getPercentage() {
      System.out.println(10000/historyMarks);
   }
}

public class AllStudent {
   public static void main(String[] args) {

   ScienceStudent abhi = new ScienceStudent("Abhishek",2,95);
   HistoryStudent raj = new HistoryStudent("Rajath",2,80);
   abhi.getPercentage();
   raj.getPercentage();
   }
}

我在" AllStudent"中收到了这个错误。那个"隐式超级构造函数Student()未定义为默认构造函数。必须定义一个显式构造函数。搜索了差异,但没有任何帮助理解和纠正问题。 它还说" AllStudent类型必须实现继承的抽象方法Student.getPercentage(); 有人可以解决这个问题并解释我应该做些什么来纠正这个问题。

1 个答案:

答案 0 :(得分:2)

您的代码格式不正确。您的代码存在以下问题:

    第一行;package implementation 中缺少
  • int ScienceMarks
  • 每个班级都缺少结束括号}
  • class类缺少
  • HistoryStudent个关键字。

以下是更正后的代码。看到它正常工作here

package implementation;

public abstract class Student {
String name; int standard;

abstract void getPercentage();
static void getTotalNoOfStudents() {}

public Student() {}
public Student(String name, int standard) {
    this.name=name; this.standard=standard;
}
}

public class ScienceStudent extends Student {
int ScienceMarks; 
public static int noOfStudents=0;

public ScienceStudent(String name, int standard, int ScienceMarks) {
    super(name, standard);
    this.ScienceMarks=ScienceMarks;
}

public void getPercentage() {
    System.out.println(10000/ScienceMarks);
}
}

public class HistoryStudent extends Student {
int historyMarks;
public static int noOfStudents;

public HistoryStudent(String name, int standard, int historyMarks) {
    super(name, standard);
    this.historyMarks=historyMarks;
}

public void getPercentage() {
    System.out.println(10000/historyMarks);
}
}

public class AllStudent {
public static void main(String[] args) {

ScienceStudent abhi = new ScienceStudent("Abhishek",2,95);
HistoryStudent raj = new HistoryStudent("Rajath",2,80);
abhi.getPercentage();
raj.getPercentage();
}
}