声明测试类,生成若干个Student类、StudentXW类及StudentBZ类对象,并分别计算它们的评测成绩
时间: 2023-10-06 15:12:21 浏览: 43
假设我们已经有了以下三个类:Student、StudentXW、StudentBZ。其中,Student为基类,StudentXW和StudentBZ为派生类。我们需要在测试类中生成这些对象,并分别计算它们的评测成绩。
首先,我们需要定义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
```
接下来,我们需要定义StudentXW和StudentBZ类,它们分别继承自Student类。这里我们假设StudentXW和StudentBZ的评测成绩计算方式与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
```