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

该代码实例展示了如何在Java中声明和使用一个名为`Student`的类,包含学号、姓名、各科成绩及总成绩等属性,并实现了计算评测成绩、比较总成绩等功能。此外,还定义了`StudentXW`和`StudentBZ`两个子类,分别代表学习委员和班长,增加了责任属性并重写了评测成绩计算方法。在测试类中创建了不同类型的对象并进行了评测成绩计算和总成绩比较。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

(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<s2.totalScore)
                System.out.println(s1.name+"'s total score is lower than "+s2.name);
            if(s1.totalScore==s2.totalScore)
                System.out.println(s1.name+"'s total score is equal to "+s2.name);
        }
        void sum(){
            totalScore=englishScore+mathScore+computerScore;
        }
        public double testScore(){
            return(totalScore/3);
        }
    }
    class StudentXW extends Student{
        private String responsibility;
        public void setResponsibility(String responsibility) {
            this.responsibility = responsibility;
        }

        public String getResponsibility() {
            return responsibility;
        }

        @Override
        public double testScore() {
            return super.testScore()+3;
        }
    }
    class StudentBZ extends  Student{
        private String responsibility;
        public void setResponsibility(String responsibility) {
            this.responsibility = responsibility;
        }

        public String getResponsibility() {
            return responsibility;
        }

        @Override
        public double testScore() {
            return super.testScore()+5;
        }
    }
}

运行结果

在这里插入图片描述

假设我们已经有了以下三个StudentStudentXWStudentBZ。其中,Student为基StudentXWStudentBZ为派生。我们需要在测试中生成这些对象,并分别计算它们的评测成绩。 首先,我们需要定义Student,包含学生的基本信息和计算评测成绩的函数calculate_score()。 ```python class Student: def __init__(self, name, age, score): self.name = name self.age = age self.score = score def calculate_score(self): # 计算评测成绩的公式 return self.score * 1.2 ``` 接下来,我们需要定义StudentXWStudentBZ,它们分别继承Student。这里我们假设StudentXWStudentBZ的评测成绩计算方式与Student不同。 ```python class StudentXW(Student): def calculate_score(self): # 计算评测成绩的公式 return self.score * 1.5 + 10 class StudentBZ(Student): def calculate_score(self): # 计算评测成绩的公式 return self.score * 1.3 + 20 ``` 最后,我们需要在测试中生成若干个对象,并分别计算它们的评测成绩。 ```python class Test: def __init__(self): # 生成对象 self.student1 = Student("张三", 18, 90) self.student2 = StudentXW("李四", 19, 85) self.student3 = StudentBZ("王五", 20, 95) def run(self): # 计算评测成绩 score1 = self.student1.calculate_score() score2 = self.student2.calculate_score() score3 = self.student3.calculate_score() # 输出结果 print(f"{self.student1.name}的评测成绩为{score1}") print(f"{self.student2.name}的评测成绩为{score2}") print(f"{self.student3.name}的评测成绩为{score3}") ``` 这样,我们就完成了测试的编写。可以通过以下代码运行程序,查看结果。 ```python test = Test() test.run() ``` 输出结果如下: ``` 张三的评测成绩为108.0 李四的评测成绩为132.5 王五的评测成绩为133.0 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值