集册 Java实例教程 创建类层次结构

创建类层次结构

欢马劈雪     最近更新时间:2020-01-02 10:19:05

522
创建类层次结构

     


import java.util.*;/*from nowjava.com - 时  代  Java*/


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/* 来自 nowjava.com - 时代Java*/

     public void printPerson(){

           System.out.println(

                    "Name: " + lastName + ", " + firstName 

               +      "\nID: " + idNumber); 

     }

      

}

class Student extends Person{

     private int[] testScores;

    protected String firstName;

     protected String lastName;

     protected int idNumber;

    public int i=0;

    

    Student(String firstName, String lastName, int identification,int[] testScores){

        super(firstName, lastName, identification);

        this.firstName = firstName;

        this.lastName = lastName;

        this.idNumber = idNumber;

        this.testScores=testScores;      

     }

    public char calculate(){

        int average = 0;

        for(int i=0;i<testScores.length;i++){

            average = average + testScores[i];

        }

        average = average / (testScores.length);

        if(average<40){

            return 'T';

        }else if(average>=40&& average<55){

            return 'D';

        }else if(average>=55&& average<70){

            return 'P';

        }else if(average>=70&& average<80){

            return 'A';

        }else if(average>=80&& average<90){

            return 'E';

        }else if(average>=90&& average<=100){

            return 'O';

        }else{

            return 0;

        }

    } 

   

}

public class Main {

     public static void main(String[] args) {

          Scanner scan = new Scanner(System.in);

          String firstName = scan.next();

          String la
展开阅读全文