栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 
运行结果 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/459262.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号