GoF大话单例单例 静态方法+静态字段

# GoF设计模式
# 大话设计模式
# 当所有实例中封装的数据都相同时,单例
# 静态方法+静态字段
import pickle
class Teacher:
def __init__(self,name,age):
self.name = name
self.age = age
self.__asset = 0
class Course:
def __init__(self,name,time,teacher):
self.name = name
self.time = time
self.teacher = teacher
def main():
t1 = Teacher("marry", 30)
t2 = Teacher("may", 31)
c1 = Course("语文", "8:00", t1)
c2 = Course("数学", "9:00", t2)
course_list = []
course_list.append(c1)
course_list.append(c2)
pickle.dump(course_list, open("course_list", "wb"))
if __name__ == '__main__':
main()
import pickle
from day.day1 import Course,Teacher
class Student:
def __init__(self):
pass

def login(self):
username = input("username:")
password = input("password:")
if username == "tom" and password == "123":
print("登陆成功")
return True
return False

def choice(self):
ret = input("which course? :")
return ret

if __name__ == '__main__':
student = Student()
if student.login():
course_list = pickle.load(open("course_list","rb"))
for i,item in enumerate(course_list,1):
j = item.teacher
print(i,item.name,item.time,j.name,j.age)

ret = student.choice()
i = course_list[int(ret)-1]
print("选课成功")
pickle.dump(i,open("tom_list","wb"))






posted @ 2018-04-01 20:36  那时年少000  阅读(109)  评论(0编辑  收藏  举报