利用反射优化选课系统代码

思路:
login
判断身份 并且根据身份实例化
根据每个身份对应的类 让用户选择能够做的事情

class Manager:
OPERATE_DIC = [
(‘创造学生账号’, ‘create_student’),
(‘创建课程’,‘create_course’),
(‘查看学生信息’,‘check_student_info’),
]
def init(self,name):
self.name = name
def create_student(self):
print(‘创建学生账号’)
def create_course(self):
print(‘创建课程’)
def check_student_info(self):
print(‘查看学生信息’)

class Student:
OPERATE_DIC = [
(‘查看所有课程’, ‘check_course’),
(‘选择课程’, ‘choose_course’),
(‘查看已选择的课程’, ‘choosed_course’)
]
def init(self,name):
self.name = name
def check_course(self):
print(‘check_course’)
def choose_course(self):
print(‘choose_course’)
def choosed_course(self):
print(‘查看已选择的课程’)

def login():
username = input('user : ')
password = input(‘pwd : ‘)
with open(‘userinfo’) as f:
for line in f:
user,pwd,ident = line.strip().split(’|’) # ident = ‘Manager’
if user == username and pwd == password:
print(‘登录成功’)
return username,ident

import sys
def main():
usr,id = login()
print(‘user,id :’,usr,id)
file = sys.modules[‘main’]
cls = getattr(file,id) #Manager = getattr(当前文件,‘Manager’)
obj = cls(usr)
operate_dic = cls.OPERATE_DIC
while True:
for num,i in enumerate(operate_dic,1):
print(num,i[0])
choice = int(input(‘num >>>’))
choice_item = operate_dic[choice-1]
getattr(obj,choice_item[1])()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值