【问题标题】:getting the error "implicit super constructor Student() is undefined for default constructor. must define an explicit constructor" [duplicate]收到错误“默认构造函数未定义隐式超级构造函数 Student()。必须定义显式构造函数”[重复]
【发布时间】:2018-06-27 02:52:30
【问题描述】:
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(); 有人可以解决这个问题并解释我应该做些什么来纠正这个问题。

【问题讨论】:

    标签: java


    【解决方案1】:

    您的代码格式错误。您的代码存在以下问题:

    • ;package implementationint ScienceMarks 中缺失。
    • 每个类都缺少右括号 }
    • HistoryStudent 类缺少 class 关键字。

    以下是更正的代码。看到它工作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();
    }
    }
    

    【讨论】:

    • 投了反对票,因为您没有解释您更改了什么以及为什么需要更改。 (我什至没有发现它......)
    • @JacobWood 更新解释
    • 那很快 - 投票改变了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 2014-06-17
    • 2020-09-29
    • 2014-11-06
    相关资源
    最近更新 更多