2、编写 个程序,定义 个学生类(Student),数据成员包括学号、姓名、性别和年龄,函数setl)初始化学号、姓名、性别和年龄,show()输出学生的学号、姓名、性别和年龄,在主函数中定义对虚数组,并实现输入5个学生的学号、姓名、性别和年龄,按年龄从大到小进行排序输出。
时间: 2023-05-20 22:00:54 浏览: 471
好的,这是一个简单的学生类的代码示例:
```python
class Student:
def __init__(self, id, name, gender, age):
self.id = id
self.name = name
self.gender = gender
self.age = age
def show(self):
print("学号:", self.id)
print("姓名:", self.name)
print("性别:", self.gender)
print("年龄:", self.age)
students = []
for i in range(5):
id = input("请输入学生学号:")
name = input("请输入学生姓名:")
gender = input("请输入学生性别:")
age = int(input("请输入学生年龄:"))
student = Student(id, name, gender, age)
students.append(student)
students.sort(key=lambda x: x.age, reverse=True)
for student in students:
student.show()
```
这个程序定义了一个学生类,包括学号、姓名、性别和年龄四个数据成员,以及一个初始化函数和一个输出函数。在主函数中,我们定义了一个学生列表,通过循环输入5个学生的信息,并将它们添加到列表中。然后,我们使用sort函数按照年龄从大到小对学生列表进行排序,并依次输出每个学生的信息。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![text/x-c++](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)