Java 子类有参构造器报错

Java 子类的有参构造器报错:Implicit super constructor Person() is undefined. Must explicitly invoke another constructor

import java.util.*;

class Person {
    protected String firstName;
    protected String lastName;
    protected int idNumber;

    // Constructor
    Person(String firstName, String lastName, int identification){
        this.firstName = firstName;
        this.lastName = lastName;
        this.idNumber = identification;
    }

    // Print person data
    public void printPerson(){
         System.out.println(
                "Name: " + lastName + ", " + firstName
            +     "\nID: " + idNumber);
    }

}

class Student extends Person{
    private int[] testScores;

    /*
    *   Class Constructor
    *
    *   @param firstName - A string denoting the Person‘s first name.
    *   @param lastName - A string denoting the Person‘s last name.
    *   @param id - An integer denoting the Person‘s ID number.
    *   @param scores - An array of integers denoting the Person‘s test scores.
    */
    // Write your constructor here
    Student(String firstName, String lastName, int id,  int[] scores){

     super();   //隐藏会执行的默认构造器

        super(firstName, lastName, id);   //因为少了父类自定义构造器初始化,所以会报错

        this.firstName = firstName;
        this.lastName = lastName;
        this.idNumber = id;
        this.testScores = scores;
    }

    /*
    *   Method Name: calculate
    *   @return A character denoting the grade.
    */
    // Write your method here
    public char calculate(){
        int num = testScores.length;
        int s = 0;
        for(int score : testScores) {
            s = s + score;
        }
        s = s/num;

        char grade = ‘-‘;
        if(s>=90&&s<=100){
            grade = ‘O‘;
        }else if(s>=80&&s<90){
            grade = ‘E‘;
        }else if(s>=70&&s<80){
            grade = ‘A‘;
        }else if(s>=55&&s<70){
            grade = ‘P‘;
        }else if(s>=40&&s<55){
            grade = ‘D‘;
        }else if(s<40){
            grade = ‘T‘;
        }
        return grade;
    }
}

class Solution {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String firstName = scan.next();
        String lastName = scan.next();
        int id = scan.nextInt();
        int numScores = scan.nextInt();
        int[] testScores = new int[numScores];
        for(int i = 0; i < numScores; i++){
            testScores[i] = scan.nextInt();
        }
        scan.close();

        Student s = new Student(firstName, lastName, id, testScores);
        s.printPerson();
        System.out.println("Grade: " + s.calculate());
    }
}

原因:父类的构造方法Person()只有有参数的构造方法,也可以说   父类没有无参的构造方法(即默认的super()初始化会报错) ,这样的话,子类继承该类,就必须要显示的调用父类的构造函数,这样才能保证,编译器在将子类初始化前,父类先被初始化。

原文地址:https://www.cnblogs.com/JesseP/p/super_constructor.html

时间: 2024-11-08 01:03:04

Java 子类有参构造器报错的相关文章

java运行闪退,报错如下,是因为ole32.dll的问题吗?

报错如下,是因为ole32.dll的问题吗?## A fatal error has been detected by the Java Runtime Environment:##  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000007fefe4109be, pid=7556, tid=7952## JRE version: Java(TM) SE Runtime Environment (8.0_60-b27) (build 1.8.0

Access restriction:The type JPEGCodec is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar 报错

报错: Access restriction:The type JPEGCodec is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar   解决方法: Project -> Properties -> libraries, 先remove掉JRE System Library,然后再Add Library重新加入. 将jre项目用自己下载的jre

Java学习笔记之Scanner报错java.util.NoSuchElementException

转载自:IT学习者-螃蟹 一个方法A使用了Scanner,在里面把它关闭了.然后又在方法B里调用方法A之后就不能再用Scanner了Scanner in = new Scanner(System.in); 测试代码如下: import java.util.Scanner; /** * * @author IT学习者-螃蟹 * * */ public class ItxxzScanner { //第一次输入 public void FistTime (){ Scanner sc = new Sca

Java Socket(断开不报错版)

网上看了很多关于Socket的Demo,用起来挺好用也简单,不过都在断开连接时,都没有做好相关处理,导致每次主动断开时,会报错 如: java.net.SocketException: Socket closed at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) at java.net.Socke

es java api 设置index mapping 报错 mapping source must be pairs of fieldnames and properties definition.

java.lang.IllegalArgumentException: mapping source must be pairs of fieldnames and properties definition. 从es官网找到的 request.mapping("_doc", "{\n" + " \"_doc\": {\n" + " \"properties\": {\n" + &quo

java初级开发中的报错问题

  1.典例1 错误原因:?useUnicode=true&characterEncoding=UTxF8 纠错:其中的? 是汉语的 2.典例2 纠错原因:数据库没有打开 纠错: 我的电脑-->右键-->管理-->服务和应用程序-->服务--->找到mysql 3.典例3 纠错原因:项目启动异常 web_xml中的url_pattern里面的配置没有加斜杠 / 纠错:把斜杠加上 4.典例4 纠错原因:类路径错误 src中java类的路径和web.xml中servlet

Eclipse 运行弹出A Java Exception has occurred.并报错Exception in thread 的解决方案

这个问题是由较高版本的JDK编译的java class文件试图在较低版本的JVM上运行而产生的错误. 1.解决措施就是保证jvm(java命令)和jdk(javac命令)版本一致.如果是linux版本,则在命令行中分别输入java -version和javac -version命令来查看版本是否一致.我用的是1.7.0_80. 2.版本一致却还是还是解决不了问题,我不是直接在命令行中用javac来编译的,而是用的eclipse的编译器来编译的.因为很多编译器都自带javac,而不是采用操作系统中

Dynamic Web Module 3.0 requires Java 1.6 or newer报错

在项目的pom.xml的<build></build>标签中加入: <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <

Linux下Java运行.class文件,报错找不到或无法加载主类

classpath配置的错误,所以找不到.class文件. 原先的etc/profile中的classpath配置 export CLASSPATH=$JAVA_HOME/lib/tools.jar 更改后的: export CLASSPATH=.:$JAVA_HOME/lib/tools.jar 加了个.:,当前目录的意思. 原文地址:https://www.cnblogs.com/wangshen31/p/9823674.html