当前位置: 首页 > news >正文

Python 面向对象练习

  不多bb了,直接上代码吧。

from pprint import pprint
class Course:total_course = []def __init__(self,name,id):self.name = nameself.id = idself.is_select = FalseCourse.total_course.append(self)def __repr__(self):return (f"{__class__.__name__}("f"学科:{self.name!r},学科代码:{self.id!r})")class Students:total_students = []def __init__(self):self.selected_courses = []# Students.total_students.pop()def register(self,name,id,institution,classes,password):self.name = nameself.id = id self.institution = institutionself.classes = classesself.password = passwordStudents.total_students.append(self)def log_in(self,student_id,student_password):for i in Students.total_students:if student_id == i.id and student_password == i.password:return Truereturn Falsedef select_course(self,course):for i in Course.total_course:if i.name == course:i.is_select = Trueself.selected_courses.append((i.name,i.id))return Truereturn Falsedef withdraw_a_course(self,course):for i in range(len(self.selected_courses)):if self.selected_courses[i][0] == course:self.selected_courses.pop(0)return Truereturn Falsedef get_name_through_id(self,id):for i in Students.total_students:if i.id == id:return i.namedef get_info_through_name(self,name):for i in Students.total_students:if i == None:continueelif i.name == name:return idef __repr__(self):return (f"{__class__.__name__}("f"{self.name!r},{self.id!r},{self.institution!r},{self.classes!r},{self.password!r})")
Course.total_course.extend([Course("Python","25234"),Course("高等数学","23123"),Course("线性代数","23145"),Course("数字逻辑","89080"),Course("算法设计与分析","237234"),Course("物理","2358239"),Course("生物","43598734"),Course("化学","233521"),Course("计算机网络","45234324")
])
Students.total_students.extend([Students().register("Jace","2432","马克思主义学院","12","1234125"),Students().register("Curry","2352525","计算机学院","13","124125")
])# info = list(input("请输入所有相关个人信息(姓名,学号,学院,班级,密码):").split())
# Students().register(*info)
# print("您已注册成功并成功登录!")
# # course = input("请输入选课名称:").strip()
# # info = Students().get_info_through_name("Jace")
# # if info.select_course(course):
# #     print("选课成功!")
# pprint(Students.total_students)student_name = ""
info = ""
is_log_in = False
while True:if is_log_in == False:print("欢迎使用选课系统!")print("1. 注册学生")print("2. 登录")print("3. 选课")print("4. 退课")print("5. 查看已选课程")print("6. 查看所有课程")print("7. 退出系统")print("8. 终止程序")print("如果您未注册,请输入1注册")# 假定学生操作无误print("如果您已注册,请输入2登录")print("如果已经登陆,请输入对应序号来进行其他操作")x = int(input())if x == 1:# 假定输入无误info = list(input("请输入所有相关个人信息(姓名,学号,学院,班级,密码):").split())Students().register(*info)print("您已注册成功并成功登录!")is_log_in = Truestudent_name = info[0]elif x == 2:info = list(input("请输入学号和密码.").split())if Students().log_in(*info):print("成功登录")is_log_in = Truestudent_name = Students().get_name_through_id(info[0])else:print("学号或密码不正确,请再尝试一下.")elif x == 3:course = input("请输入选课名称:").strip()info = Students().get_info_through_name(student_name)if info.select_course(course):print("选课成功!")else:print("没有该课程.")elif x == 4:course = input("请输入退课名称:").strip()info = Students().get_info_through_name(student_name)if info.withdraw_a_course(course):print("退课成功")else:print("没有该退课课程")elif x == 5:info = Students().get_info_through_name(student_name)if info.selected_courses == []:print("您未选课")else:print(f"您学课程如下:{info.selected_courses}")elif x == 6:print("所有科目显示如下:")pprint(Course.total_course)elif x == 7:print("您已退出")student_name = ""info = ""is_log_in = Falsecontinueelif x == 8:break

  一个小题目,本来使用列表+字典实现的,我看了想了想,还是通过面向对象来做做吧,提高一下能力。整个代码测试下来的时候就是在Students中的类变量列表total_students,在我一开始通过两个register初始化的时候,总会出现多余的两个None,不知道为啥,以及为何出现,并且在选课模块出现了问题,我自己看了看,改了.get_info_through_name的内部逻辑,如果为None的话直接continue,这样能解决问题,但是并没有根本解决问题。
  最近看了manim大佬的源代码,确实能学到很多东西,对面向对象又够了更深层次的认识。如果缺少这一认识,这个题目虽然也能用面向对象思想来做,然理解程度肯定不够。当然,现在肯定也是不够的,还需要多多摸索。manim中的类、方法,肯定是因为为了解决实际情况而产生的,对于制作它们的作者来说,理解起来很容易,但是开源让别人学的话,不明白其切实含义,缺少实践,就是很难理解。比如在我的Students类中有这两个方法:

def get_name_through_id(self,id):for i in Students.total_students:if i.id == id:return i.name
def get_info_through_name(self,name):for i in Students.total_students:if i == None:continueelif i.name == name:return i

  别人来看肯定不理解什么意思以及为什么有这两个方法,但是确实是我自己在解决问题时必要的,第一个方法是通过id找到name,即由学号找到姓名。登录时是输入学号+密码登录,通过学号找到姓名,进而在进行一些列的操作。第二个方法是通过姓名找到所有个人信息,看名字就知道啥意思了,我也不多说了。这都是有其存在的意义的。
  还是要感谢那个manim大佬,让我对抽象类有了额外的理解。无法用言语说明,只能自个去悟。

相关文章:

  • 日内组合策略思路
  • 强化学习(Reinforcement Learning, RL)和深度学习(Deep Learning, DL)
  • 数据结构——栈与队列
  • 简单场景下的目标关联算法:GNN全局最近邻与匈牙利算法
  • 制作一款打飞机游戏20:敌人被击中时的视觉效果
  • 理解js函数(Ⅱ)
  • 嵌入式Linux驱动开发:LED实验
  • Spring Boot中自定义404异常处理问题学习笔记
  • Android学习总结之Room篇
  • 发送网络请求
  • 《无尽的尽头》今日开播 刘家祎大胆演绎林磊儿的“另一面”
  • RAG(检索增强生成)技术详解与应用实践:从原理到落地
  • 简单几步,开启 Intel VT-x 让电脑“解开CPU封印”
  • 蓝桥杯 20. 压缩变换
  • 数据分析之 商品价格分层之添加价格带
  • 欧姆龙NJ系列PLC通讯
  • vue3-springboot-mysql的docker部署
  • 怎么实现RAG检索相似文档排序:similarities
  • 云蝠智能大模型呼叫:AI驱动的通信服务革新与实践
  • 操作系统---进程同步与互斥
  • 《哪吒之魔童降世》电影版权方诉《仙侠神域》游戏运营方侵权案开庭
  • 国防部就美军“压力测试”大演习答澎湃:中国从来不信邪,不怕打,不怕压
  • 冲击一英里4分钟大关,基普耶贡挑战女子中长跑极限
  • 泽连斯基提议乌俄“立即、全面和无条件”停火
  • 舞剧《百合花》7月绽放,王安忆:这是送给母亲的一份礼物
  • 对话地铁读书人|超市营业员朱先生:通勤时间自学心理学