#简化选课系统代码:先登陆,然后判断身份并实例化,根据身份对应的类,让用户选择 class Manager: operate_dict=[ ('创造学生账号',"creat_student"), ('查看学生信息',"check_student") ] def __init__(self,name): self.name=name def creat_student(self): print('创建学生账号') def check_student(self): print('查看学生信息') class Student: def __init__(self, name): self.name = name def check(cls): print('查看课程') def login(cls): print('登录') class Teacher: def __init__(self, name): self.name = name def login(): username=input('user:') password=input('password:') with open('userinfo')as f: for line in f : user,pwd,ident=line.strip().split('|') if user==username and pwd==password: print('登陆成功') return username,ident import sys def main(): usr,id=login() file=sys.modules['__main__'] cls=getattr(file,id) obj=cls(usr) Operate_dict=cls.operate_dict print(Operate_dict) for num,item in enumerate(Operate_dict): print(num,item[0]) choice=int(input("num>>>")) choice_item=Operate_dict[choice-1] getattr(obj.choice_item[1])() main()
这个Demo不涉及数据库和JS等内容,只是单纯考察反射知识点.
接下来进行重点部分的分析
第一次跟着写,分析也是云里雾里的.可能还会更新吧(?)
________________________________________________________________________________
20190921更新一波
搭配代码开发规范可以写出相对完整的框架.
比如这样写一个框架的开头: