Python3 类的继承小练习
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
第一行: 子类会继承父类的属性
第二行: Child1.x = 2
给Child1
类添加了派生属性, 属性的搜索顺序是先子类后父类
第三行: Parent.x = 3
修改了父类的属性, Child2
继承了Parent
的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
D
A
B
F
C
B
D
A
- 调用类会自动触发
__init__
函数 - 新式类的多重继承按从左到右, 广度优先的顺序搜索
3、什么是新式类,什么是经典类,二者有什么区别?什么是深度优先,什么是广度优先?
- 新式类: 所有继承object的类及其子类都是新式类
- 经典类: Python2没有继承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、按照定义老师的方式,再定义一个学生类
import hashlib
import time
import pickle
# 老师类
class PyTeacher:
def __init__(self, name, gender, age, level, salary):
self.name = name
self.gender = gender
self.age = age
self.level = level
self.salary = salary
self.id = self.creat_id()
def creat_id(self):
raw_id = str(time.time()) + self.name + self.gender + str(self.age) + str(self.level) + str(self.salary)
id = hashlib.md5(raw_id.encode()).hexdigest()
return id
def teach(self):
print('teaching Python...')
def tell_info(self):
print(f'ID:{teacher.id}, name:{teacher.name}, gender:{teacher.gender}, age:{teacher.age}, level:{teacher.level}, salary:{teacher.salary}')
def save(self):
with open(self.id, 'wb') as f:
pickle.dump(self, f)
print('done!')
def get_obj_by_id(self):
return pickle.load(open(self.id, 'rb'))
teacher = PyTeacher('龟叔', 'male', 18, 10, 0)
# 学生类
class PyStudent:
def __init__(self, name, gender, age, class_no, score):
self.name = name
self.gender = gender
self.age = age
self.class_no = class_no
self.score = score
self.id = self.creat_id()
def creat_id(self):
raw_id = str(time.time()) + self.name + self.gender + str(self.age) + str(self.class_no) + str(self.score)
id = hashlib.md5(raw_id.encode()).hexdigest()
return id
def learn(self):
print('learning Python...')
def tell_info(self):
print(f'''
ID:{self.id},
name:{self.name},
gender:{self.gender},
age:{self.age},
class:{self.class_no},
score:{self.score}
''')
def save(self):
with open(self.id, 'wb') as f:
pickle.dump(self, f)
print('done!')
def get_obj_by_id(self):
return pickle.load(open(self.id, 'rb'))
5、抽象老师类与学生类得到父类,用继承的方式减少代码冗余
class PyMan:
language = 'Python'
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def save(self):
with open(self.id, 'wb') as f:
pickle.dump(self, f)
print('done!')
def get_obj_by_id(self):
return pickle.load(open(self.id, 'rb'))
# 老师类
class PyTeacher(PyMan):
def __init__(self, name, age, gender, level, salary):
super().__init__(self, name, age, gender)
self.level = level
self.salary = salary
self.id = self.creat_id()
def creat_id(self):
raw_id = str(time.time()) + self.name + self.gender + str(self.age) + str(self.level) + str(self.salary)
id = hashlib.md5(raw_id.encode()).hexdigest()
return id
def teach(self):
print('teaching Python...')
def tell_info(self):
print(f'''
ID:{teacher.id},
name:{teacher.name},
gender:{teacher.gender},
age:{teacher.age},
level:{teacher.level},
salary:{teacher.salary}
''')
# 学生类
class PyStudent(PyMan):
def __init__(self, name, age, gender, class_no, score):
super().__init__(self, name, age, gender)
self.class_no = class_no
self.score = score
self.id = self.creat_id()
def learn(self):
print('learning Python...')
def tell_info(self):
print(f'''
ID:{self.id},
name:{self.name},
gender:{self.gender},
age:{self.age},
class:{self.class_no},
score:{self.score}
''')