面向对象习题
发布人:shili8
发布时间:2024-12-27 13:13
阅读次数:0
**面向对象程序设计习题**
###习题1:学生信息管理系统#### 题目描述设计一个学生信息管理系统,包括以下功能:
* 添加学生信息* 删除学生信息* 修改学生信息* 查询学生信息* 显示所有学生信息#### 类定义
class Student: def __init__(self, name, age, grade): self.name = name self.age = age self.grade = gradeclass StudentManager: def __init__(self): self.students = [] def add_student(self, student): self.students.append(student) def delete_student(self, name): for i in range(len(self.students)): if self.students[i].name == name: del self.students[i] break def modify_student(self, old_name, new_name, age, grade): for i in range(len(self.students)): if self.students[i].name == old_name: self.students[i].name = new_name self.students[i].age = age self.students[i].grade = grade break def query_student(self, name): for student in self.students: if student.name == name: return student return None def display_all_students(self): for student in self.students: print(f"姓名:{student.name}, 年龄:{student.age}, 年级:{student.grade}")
#### 使用示例
manager = StudentManager() # 添加学生信息manager.add_student(Student("张三",20, "高一")) manager.add_student(Student("李四",21, "高二")) # 删除学生信息manager.delete_student("张三") # 修改学生信息manager.modify_student("李四", "王五",22, "高三") # 查询学生信息student = manager.query_student("王五") if student: print(f"姓名:{student.name}, 年龄:{student.age}, 年级:{student.grade}") else: print("未找到学生信息") # 显示所有学生信息manager.display_all_students()
###习题2:图书管理系统#### 题目描述设计一个图书管理系统,包括以下功能:
* 添加图书信息* 删除图书信息* 修改图书信息* 查询图书信息* 显示所有图书信息#### 类定义
class Book: def __init__(self, title, author, price): self.title = title self.author = author self.price = priceclass BookManager: def __init__(self): self.books = [] def add_book(self, book): self.books.append(book) def delete_book(self, title): for i in range(len(self.books)): if self.books[i].title == title: del self.books[i] break def modify_book(self, old_title, new_title, author, price): for i in range(len(self.books)): if self.books[i].title == old_title: self.books[i].title = new_title self.books[i].author = author self.books[i].price = price break def query_book(self, title): for book in self.books: if book.title == title: return book return None def display_all_books(self): for book in self.books: print(f"书名:{book.title},作者:{book.author}, 价格:{book.price}")
#### 使用示例
manager = BookManager() # 添加图书信息manager.add_book(Book("Python编程", "张三",50)) manager.add_book(Book("Java编程", "李四",60)) # 删除图书信息manager.delete_book("Python编程") # 修改图书信息manager.modify_book("Java编程", "C++编程", "王五",70) # 查询图书信息book = manager.query_book("C++编程") if book: print(f"书名:{book.title},作者:{book.author}, 价格:{book.price}") else: print("未找到图书信息") # 显示所有图书信息manager.display_all_books()
###习题3:员工管理系统#### 题目描述设计一个员工管理系统,包括以下功能:
* 添加员工信息* 删除员工信息* 修改员工信息* 查询员工信息* 显示所有员工信息#### 类定义
class Employee: def __init__(self, name, age, department): self.name = name self.age = age self.department = departmentclass EmployeeManager: def __init__(self): self.employees = [] def add_employee(self, employee): self.employees.append(employee) def delete_employee(self, name): for i in range(len(self.employees)): if self.employees[i].name == name: del self.employees[i] break def modify_employee(self, old_name, new_name, age, department): for i in range(len(self.employees)): if self.employees[i].name == old_name: self.employees[i].name = new_name self.employees[i].age = age self.employees[i].department = department break def query_employee(self, name): for employee in self.employees: if employee.name == name: return employee return None def display_all_employees(self): for employee in self.employees: print(f"姓名:{employee.name}, 年龄:{employee.age}, 部门:{employee.department}")
#### 使用示例
manager = EmployeeManager() # 添加员工信息manager.add_employee(Employee("张三",30, "销售部")) manager.add_employee(Employee("李四",31, "研发部")) # 删除员工信息manager.delete_employee("张三") # 修改员工信息manager.modify_employee("李四", "王五",32, "人事部") # 查询员工信息employee = manager.query_employee("王五") if employee: print(f"姓名:{employee.name}, 年龄:{employee.age}, 部门:{employee.department}") else: print("未找到员工信息") # 显示所有员工信息manager.display_all_employees()
###习题4:学生成绩管理系统#### 题目描述设计一个学生成绩管理系统,包括以下功能:
* 添加学生成绩信息* 删除学生成绩信息* 修改学生成绩信息* 查询学生成绩信息* 显示所有学生成绩信息#### 类定义
class Student: def __init__(self, name): self.name = name self.scores = [] class ScoreManager: def __init__(self): self.students = {} def add_score(self, student_name, score): if student_name not in self.students: self.students[student_name] = [] self.students[student_name].append(score) def delete_score(self, student_name, score): if student_name in self.students and score in self.students[student_name]: self.students[student_name].remove(score) if len(self.students[student_name]) ==0: del self.students[student_name] def modify_score(self, old_student_name, new_student_name, old_score, new_score): if old_student_name in self.students and old_score in self.students[old_student_name]: self.students[new_student_name].append(new_score) self.students[old_student_name].remove(old_score) def query_score(self, student_name, score): if student_name in self.students and score in self.students[student_name]: return True return False def display_all_scores(self): for student_name, scores in self.students.items(): print(f"学生:{student_name}, 成绩:{scores}")
#### 使用示例
manager = ScoreManager() # 添加学生成绩信息manager.add_score("张三",90) manager.add_score("李四",80) # 删除学生成绩信息manager.delete_score("张三",90) # 修改学生成绩信息manager.modify_score("李四", "王五",80,70) # 查询学生成绩信息if manager.query_score("王五",70): print("找到学生成绩") else: print("未找到学生成绩") # 显示所有学生成绩信息manager.display_all_scores()
###习题5:图书分类管理系统#### 题目描述设计一个图书分类管理系统,包括以下功能:
* 添加图书分类信息* 删除图书分类信息* 修改图书分类信息* 查询图书分类信息* 显示所有图书分类信息#### 类定义
class BookCategory: def __init__(self, name): self.name = name self.books = [] class CategoryManager: def __init__(self): self.categories = {}