标签:open get 面向 create imp iges 特征 main 类的属性
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
属性
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__
函数 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 PyMan:
language = 'Python'
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
# 老师类
class PyTeacher(PyMan):
def __init__(self, name, age, gender, salary):
super().__init__(self, name, age, gender)
self.salary = salary
def teach_python(self):
print(f'老师[{self.name}]在教Python...')
# 学生类
class PyStudent(PyMan):
def __init__(self, name, age, gender, class_no):
super().__init__(self, name, age, gender)
self.class_no = class_no
def learn_python(self):
print(f'学生[{self.name}]在学Python...')
标签:open get 面向 create imp iges 特征 main 类的属性
原文地址:https://www.cnblogs.com/bigb/p/11650707.html