今日作业
1、下面这段代码的输出结果将是什么?请解释。
class Parent(object):
x = 1
class Child1(Parent):
pass
class Child2(Parent):
pass
print(Parent.x, Child1.x, Child2.x)
Child1.x = 2
print(Parent.x, Child1.x, Child2.x)
Parent.x = 3
print(Parent.x, Child1.x, Child2.x)
1 1 1
1 2 1
3 2 3
答: Parent是child1,childl2的父类,在当Child1.x, Child2.x时,Child1, Child2内没有x,使用父类中x.即x =1;
当child1.x=2时,在Child1中找到x =2,则不会去父类找;
当Parent.x =3时child2没有x,所以去父类找x,而child1内有x,则不会去父类中找。
2、多重继承的执行顺序,请解答以下输出结果是什么?并解释。
class A(object):
def __init__(self):
print('A')
super(A, self).__init__()
class B(object):
def __init__(self):
print('B')
super(B, self).__init__()
class C(A):
def __init__(self):
print('C')
super(C, self).__init__()
class D(A):
def __init__(self):
print('D')
super(D, self).__init__()
class E(B, C):
def __init__(self):
print('E')
super(E, self).__init__()
class F(C, B, D):
def __init__(self):
print('F')
super(F, self).__init__()
class G(D, B):
def __init__(self):
print('G')
super(G, self).__init__()
if __name__ == '__main__':
g = G()
f = F()
g : G D A B 深度优先---》经典类
f : F C B D A 广度优先--》新式类
3、什么是新式类,什么是经典类,二者有什么区别?什么是深度优先,什么是广度优先?
答: 新式类:继承了object的类以及该类的子类
经典类:没有继承了object的类以及该类的子类
区别:继承搜索的顺序发生了改变,
深度优先:先深入继承树左侧查找,然后再返回,开始查找右侧
广度优先:先在水平方向查找,然后再向上查找
4、用面向对象的形式编写一个老师类, 老师有特征:编号、姓名、性别、年龄、等级、工资,老师类中有功能。
1.生成老师唯一编号的功能,可以用hashlib对当前时间加上老师的所有信息进行校验得到一个hash值来作为老师的编号
def create_id(self):
pass
2.获取老师所有信息
def tell_info(self):
pass
3.将老师对象序列化保存到文件里,文件名即老师的编号,提示功能如下
def save(self):
with open('老师的编号','wb') as f:
pickle.dump(self,f)
4.从文件夹中取出存储老师对象的文件,然后反序列化出老师对象,提示功能如下
def get_obj_by_id(self,id):
return pickle.load(open(id,'rb'))
5、按照定义老师的方式,再定义一个学生类
6、抽象老师类与学生类得到父类,用继承的方式减少代码冗余
#4;
import hahlib
import pickle
import time
class Teacher_Student:
def __init__(self,name,sex,age,level,salary):
self.name = name
self.sex = sex
self.age = age
self.level = level
self.salary = salary
def create_id(self):
create_id_msg = (f'{self.name},{self.sex},{self.age},{self.level},{self.salary}')
create_id_msg = create_id_msg.encode('utf-8')
create_time = str(time.time()).encode('utf-8')
teacher_id = hashlib.md5()
teacher_id.update(create_time)
teacher_id.update(create_id_msg)
return teacher_id.hexdigest()
def tell_info(self):
print("""
姓名: self.name,
年龄: self.age,
性别: self.sex,
等级: self.level,
薪资: self.salary,
""")
def save(self,id):
with open(id,'wb') as f:
pickle.dump(self,f)
def get_obj_by_id(self,id):
return pickle.load(open(id,'rb'))
#5.
import hahlib
import pickle
import time
class Student:
def __init__(self,name,sex,age,):
self.name = name
self.sex = sex
self.age = age
def create_id(self):
create_id_msg = (f'{self.name},{self.sex},{self.age}')
create_id_msg = create_id_msg.encode('utf-8')
create_time = str(time.time()).encode('utf-8')
student = hashlib.md5()
student_id.update(create_time)
student_id.update(create_id_msg)
return student_id.hexdigest()
def tell_info(self):
print("""
姓名: self.name,
年龄: self.age,
性别: self.sex,
""")
def save(self,id):
with open(id,'wb') as f:
pickle.dump(self,f)
def get_obj_by_id(self,id):
return pickle.load(open(id,'rb'))
#6.
import hahlib
import pickle
import time
class Teacher_Student:
def __init__(self,name,sex,age,):
self.name = name
self.sex = sex
self.age = age
def create_id(self):
create_id_msg = (f'{self.name},{self.sex},{self.age}')
create_id_msg = create_id_msg.encode('utf-8')
create_time = str(time.time()).encode('utf-8')
student = hashlib.md5()
student_id.update(create_time)
student_id.update(create_id_msg)
return student_id.hexdigest()
def tell_info(self):
print("""
姓名: self.name,
年龄: self.age,
性别: self.sex,
""")
def save(self,id):
with open(id,'wb') as f:
pickle.dump(self,f)
def get_obj_by_id(self,id):
return pickle.load(open(id,'rb'))
class Teacher(Teacher_Student):
def __init__(self,name,age,sex,level,saraly):
super().__init__(name,age,sex)
self.level = level
self.salsry = salrary
def create_id(self):
create_id_msg = (f'{self.name},{self.sex},{self.age},{self.level},{self.salary}')
create_id_msg = create_id_msg.encode('utf-8')
create_time = str(time.time()).encode('utf-8')
teacher_id = hashlib.md5()
teacher_id.update(create_time)
teacher_id.update(create_id_msg)
return teacher_id.hexdigest()
def tell_info(self):
super().tell_info()
print("""
等级: self.level
薪资: self.salary
""")