练习_类的继承派生
Posted allenchen168
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了练习_类的继承派生相关的知识,希望对你有一定的参考价值。
1、下面这段代码的输出结果将是什么?请解释。
class Parent(object): # 定义一个父类
x = 1
class Child1(Parent): # 定义一个子类1
pass
class Child2(Parent): # 定义一个子类2
pass
print(Parent.x, Child1.x, Child2.x)
# 打印结果为:1 1 1
# 类child1、child2都继承了父类Parent,子类中没有x,就去父类中找到x
Child1.x = 2
print(Parent.x, Child1.x, Child2.x)
# Child1调用了父类的X并给其赋值2
# 打印结果为:1 2 1
Parent.x = 3
print(Parent.x, Child1.x, Child2.x)
# 父类的x值变为3,Child1调用父类x并给其赋值2,Child2调用父类的x
# 打印结果为:3 2 3
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-object
# f的执行顺序:F-C-D-A-B-object
3、什么是新式类,什么是经典类,二者有什么区别?什么是深度优先,什么是广度优先?
答:python2中才会有这两种区分,继承object的类都是新式类,凡是没有继承object的都是经典类
? 在python3中,所有类都是新式类,因为python3中所有没指定父类的子类都默认继承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'))
import hashlib
import time
import pickle
class Teacher:
def __init__(self, name, gender, age, level, salary):
self.name = name
self.gender = gender
self.age = age
self.level = level
self.salary = salary
def create_id(self):
m = hashlib.md5()
msg = str(self.__dict__)
m.update(msg.encode('utf8'))
t1.num = m.hexdigest()
def tell_info(self):
print(self.__dict__)
def save(self):
with open(t1.num, 'wb') as f:
pickle.dump(self.__dict__, f)
def get_obj_by_id(self):
return pickle.load(open(self.num, 'rb'))
t1 = Teacher('allen', 'male', 22, 5, 20000)
t1.create_id()
t1.tell_info()
t1.save()
print(t1.get_obj_by_id())
5、按照定义老师的方式,再定义一个学生类
import hashlib
import time
import pickle
class Students:
def __init__(self, name, gender, age):
self.name = name
self.gender = gender
self.age = age
def create_id(self):
m = hashlib.md5()
msg = str(self.__dict__)
m.update(msg.encode('utf8'))
s1.num = m.hexdigest()
def tell_info(self):
print(self.__dict__)
def save(self):
with open(s1.num, 'wb') as f:
pickle.dump(self.__dict__, f)
def get_obj_by_id(self):
return pickle.load(open(self.num, 'rb'))
s1 = Students('allen', 'male', 22)
s1.create_id()
s1.tell_info()
s1.save()
print(s1.get_obj_by_id())
6、抽象老师类与学生类得到父类,用继承的方式减少代码冗余
import hashlib
import time
import pickle
class People:
def __init__(self, name, gender, age):
self.name = name
self.gender = gender
self.age = age
def create_id(self):
m = hashlib.md5()
msg = str(self.__dict__)
m.update(msg.encode('utf8'))
t1.num = m.hexdigest()
def tell_info(self):
print(self.__dict__)
def save(self):
with open(t1.num, 'wb') as f:
pickle.dump(self.__dict__, f)
def get_obj_by_id(self):
return pickle.load(open(self.num, 'rb'))
class Teacher(People):
def __init__(self, name, gender, age, level, salary):
super().__init__(name, gender, age)
self.level = level
self.salary = salary
class Students(People):
def __init__(self, name, gender, age):
super().__init__(name, gender, age)
t1 = Teacher('allen', 'male', 22, 5, 20000)
s1 = Students('Frank', 'male', 23)
t1.create_id()
t1.tell_info()
t1.save()
print(t1.get_obj_by_id())
以上是关于练习_类的继承派生的主要内容,如果未能解决你的问题,请参考以下文章