Java中类的继承及其应用(2)

Java中类的继承及其应用(2),第1张

Java中类的继承及其应用(2) 问题描述

(1)声明Student类。
属性包括学号(number)、姓名(name)、英语成绩(englishScore)、数学成绩(mathScore)、计算机成绩(computerScore)和总成绩(totalScore)。
方法包括构造方法、get方法、set方法、compare方法(比较两个学生的总成绩,结果分大于、小于、等于)、sum方法(计算总成绩)和testScore方法(计算评测成绩)。
注:评测成绩可以取三门课成绩的平均分,另外任何一门课的成绩的改变都需要对总成绩进行重新计算。
(2)声明StudentXW(学习委员)类为Student类的子类。
在StudentXW类中增加责任(responsibility)属性,并重写testScore方法(计算评测成绩,评测成绩=三门课的平均分+3)。
(3)声明StudentBZ(班长)类为Student类的子类。
在StudentBZ类中增加责任(responsibility)属性,并重写testScore方法(计算评测成绩,评测成绩=三门课的平均分+5)
(4)声明测试类,生成若干个Student类、StudentXW类及StudentBZ类对象,并分别计算它们的评测成绩,以及互相进行总成绩的对比。

完整代码
public class Main {
    public static void main(String[] args) {
        Student s1=new Student(12,"Lisi",70,70,70);
        s1.sum();
        System.out.println(s1.getName()+"'s evaluation score is " +s1.testScore());
        Student s2=new Student(13,"Zhangsan",93,93,93);
        s2.sum();
        System.out.println(s2.getName()+"'s evaluation score is " +s2.testScore());
        Student s3=new Student(14,"Wangwu",105,105,105);
        s3.sum();
        System.out.println(s3.getName()+"'s evaluation score is " +s3.testScore());
        Student s4=new Student(15,"Zhaoliu",70,70,70);
        s4.sum();
        System.out.println(s4.getName()+"'s evaluation score is " +s4.testScore());
        System.out.println();
        s4.compare(s2,s1);
        s4.compare(s2,s3);
        s4.compare(s1,s4);
    }
    static class Student{
        private int number;
        private String name;
        private double englishScore;
        private double mathScore;
        private double computerScore;
        private double totalScore;
        public Student(){}
        public Student(int number,String name,double englishScore,double mathScore,double computerScore){
            this.number=number;
            this.name=name;
            this.englishScore=englishScore;
            this.mathScore=mathScore;
            this.computerScore=computerScore;
        }

        public void setNumber(int number) {
            this.number = number;
        }

        public int getNumber() {
            return number;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setEnglishScore(double englishScore) {
            this.englishScore = englishScore;
        }

        public double getEnglishScore() {
            return englishScore;
        }

        public void setMathScore(double mathScore) {
            this.mathScore = mathScore;
        }

        public double getMathScore() {
            return mathScore;
        }

        public void setComputerScore(double computerScore) {
            this.computerScore = computerScore;
        }

        public double getComputerScore() {
            return computerScore;
        }

        void compare(Student s1,Student s2){
            if(s1.totalScore>s2.totalScore)
                System.out.println(s1.name+"'s total score is higher than "+s2.name);
            if(s1.totalScore 
运行结果 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5434776.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存