day 20 作业
01
1 1 1 # (直接从Parent内存空间中调用x=1)(Child1内存空间中未定义x属性,从它的父类Parent中调用)(Child2内存空间中未定义x属性,从它的父类Parent中调用)
1 2 1 # (直接从Parent内存空间中调用x=1)(刚刚给Child1定义了Child1.x=2,所以Child1内存空间中有了x=2,直接调用)(Child2内存空间中未定义x属性,从它的父类Parent中调用)
3 2 3 # (刚刚修改了Parent内存空间中x=1为x=3,直接调用x=3)(Child1.x=2使Child1内存空间中有了x=2,直接调用)(Child2内存空间中未定义x属性,从它的父类Parent中调用)
02
# 定义对象自动执行类下面的__init__,super调用父类__init__()
G D A B # 首先运行G类,运行D,运行A(A类继承object,G类的所有父类中只有D类继承A类)运行B
F C B D A # 因为C类和D类都继承A类,所以当C类和D类都运行结束后在运行A类
03
新式类;所有继承object的类,python3中默认继承object
经典类;没有继承object的类
深度优先;经典类继承顺序,根据括号内继承的父类,从左到右依次走到头(每个类只运行1次)
广度优先;新式类的继承顺序,根据括号内继承的父类,从左到右依次走到头(每个类只运行1次,继承object的类在所有继承该类的子类都运行结束的时候在运行)
代码
# 实现创建或查看老师和学生,没有健壮性
import hashlib
import pickle
import os
import da
class prople():
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
self.found_time = datetime.datetime.now()
num = hashlib.md5()
num.update(f'{self.name}{str(self.age)}{str(self.sex)}'.encode('utf-8'))
self.num =num.hexdigest()
def all_info(self):
return f'创建时间;{self.found_time},姓名;{self.name},年龄;{str(self.age)},性别;{self.sex}'
def save(self):
with open(f'{self.number}.pkl','wb') as fw:
pickle.dump(self,fw)
return '创建成功'
class Teacher(prople):
def __init__(self, name, age, sex, rank, wage):
super().__init__(name, age, sex)
self.rank = rank
self.wage = wage
self.number = f'T{self.num}'
def tall_info(self):
return f'{self.all_info()},等级;{self.rank},工资;{self.wage},编号;{self.number}'
class Student(prople):
def __init__(self, name, age, sex, course, money):
super().__init__(name, age, sex)
self.course = course
self.money = money
self.number = f'S{self.num}'
def tall_info(self):
return f'{self.all_info()},课程;{self.course},学费;{self.money},编号;{self.number}'
def get_info(id):
if os.path.exists(f'{id}.pkl'):
with open(f'{id}.pkl','rb')as fr:
data=pickle.load(fr)
return data
def found():
while True:
print('''
1 老师
2 学生
输入选择;
''')
choice = input()
if choice == 'q':
break
name = input('名字;')
age = int(input('年龄;'))
sex = input('性别;')
if choice == '1':
rank = int(input('等级;'))
wage = int(input('工资;'))
num = hashlib.md5()
num.update(f'{name}{age}{sex}'.encode('utf-8'))
tid = f'T{num.hexdigest()}'
if get_info(tid):
print('该角色已创建')
continue
T = Teacher(name, age, sex, rank, wage)
msg = T.save()
print(msg)
break
elif choice == '2':
course = int(input('课程;'))
money = int(input('学费;'))
num = hashlib.md5()
num.update(f'{name}{age}{sex}'.encode('utf-8'))
tid = f'S{num.hexdigest()}'
if get_info(tid):
print('该角色已创建')
continue
T = Teacher(name, age, sex, course, money)
msg = T.save()
print(msg)
break
def get_it():
while True:
choice = input('''
T 老师
S 学生
输入选择;
''')
if choice == 'q':
break
name = input('名字;')
age = int(input('年龄;'))
sex = input('性别;')
num = hashlib.md5()
num.update(f'{name}{age}{sex}'.encode('utf-8'))
tid = f'{choice}{num.hexdigest()}'
print(tid)
if not get_info(tid):
print('没有该角色')
continue
fd = get_info(tid)
print(fd.tall_info())
break
def run():
while True:
choice = input('''
1 创建角色
2 查看角色
输入选择;
''')
if choice=='q':
break
if choice=='1':
found()
elif choice=='2':
get_it()
if __name__ == '__main__':
run()