综合应用Python程序设计的知识实现下面要求。 (1)设计一个Person类,类变量有id,成员变量有name、sex、age、height、weight (2)实现成员方法1:打印输出属性值 (3)实现静态方法:输出类变量id的值 (4)在Person类中重载__repr__方法 (5)设计子类学生Student(继承Person类),成员属性除了name、sex、age、height、weight之外,增加学号student_number,年级grade,并重写成员方法1,打印所有的属性值 (6)学生的集合类StudentList,它可以承载多名学生信息,并重载索引和切片的方法(__getitem__、__setitem__、__delitem__) (7)设计子类教师Teacher(继承Person类),成员属性除了name、sex、age、height、weight之外,增加教学teach,管理manage,并重写成员方法1,打印所有的属性值 (8)在Teacher类中,对属性age重载__ge__方法 (9)期待同学们设计实现其他成员方法 (10)系统功能完整,程序运行稳定,打包成独立的exe文件
时间: 2024-04-03 09:37:15 浏览: 60
以下是 Python 程序设计的实现:
```python
class Person:
id = 0
def __init__(self, name, sex, age, height, weight):
self.name = name
self.sex = sex
self.age = age
self.height = height
self.weight = weight
Person.id += 1
def __repr__(self):
return f"{self.__class__.__name__}(name='{self.name}', sex='{self.sex}', age={self.age}, height={self.height}, weight={self.weight})"
def print_info(self):
print(f"Name: {self.name}")
print(f"Sex: {self.sex}")
print(f"Age: {self.age}")
print(f"Height: {self.height}")
print(f"Weight: {self.weight}")
@staticmethod
def print_id():
print(f"ID: {Person.id}")
class Student(Person):
def __init__(self, name, sex, age, height, weight, student_number, grade):
super().__init__(name, sex, age, height, weight)
self.student_number = student_number
self.grade = grade
def __repr__(self):
return f"{self.__class__.__name__}(name='{self.name}', sex='{self.sex}', age={self.age}, height={self.height}, weight={self.weight}, student_number='{self.student_number}', grade={self.grade})"
def print_info(self):
super().print_info()
print(f"Student Number: {self.student_number}")
print(f"Grade: {self.grade}")
class Teacher(Person):
def __init__(self, name, sex, age, height, weight, teach, manage):
super().__init__(name, sex, age, height, weight)
self.teach = teach
self.manage = manage
def __repr__(self):
return f"{self.__class__.__name__}(name='{self.name}', sex='{self.sex}', age={self.age}, height={self.height}, weight={self.weight}, teach='{self.teach}', manage='{self.manage}')"
def print_info(self):
super().print_info()
print(f"Teach: {self.teach}")
print(f"Manage: {self.manage}")
def __ge__(self, other):
return self.age >= other.age
class StudentList:
def __init__(self, *students):
self.students = list(students)
def __getitem__(self, index):
return self.students[index]
def __setitem__(self, index, value):
self.students[index] = value
def __delitem__(self, index):
del self.students[index]
```
你可以使用 `pyinstaller` 将 Python 程序打包成独立的 exe 文件,具体的操作可以参考官方文档:https://pyinstaller.readthedocs.io/en/stable/usage.html
阅读全文