Java类继承构造函数问题

时间:2018-11-17 12:26:23

标签: java class inheritance constructor subclass

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
    */
    Student(String fName, String lName, int id, int[] scores){
        super(fName, lName, id);
        for(int i = 0; i < scores.length; i++){
            testScores[i] = scores[i];
        }
    }

    public char calculate(){
        int total = 0;
        for(int i = 0; i < testScores.length; i++){
            total = total + testScores[i];
        }
        int grade = total/testScores.length;
        char mark = 'q';
        if(grade <= 100 && grade >=90){
            mark = 'O';
        }
        else if(grade < 90 && grade >= 80){
            mark = 'E';
        }
        else if(grade < 80 && grade >= 70){
            mark = 'A';
        }
        else if(grade < 70 && grade >= 55){
            mark = 'P';
        }
        else if(grade < 55 && grade >= 40){
            mark = 'D';
        }
        else if(grade < 40){
            mark = 'T';
        }
        return mark;
    }


}

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());
    }
}

我正在练习Java类继承,并且遇到了运行上述代码的问题。我用方法和实例变量创建了一个人员类,并用一个包含标记的数组的附加实例变量扩展了该人员类的学生类。另外,还有一种基于平均值计算标记的方法。当我尝试打印学生的所有信息时,出现错误。我认为问题是不确定的子类的构造函数。你们可以帮我吗?

0 个答案:

没有答案