标签:bin print 需要 可变对象 抽象 clone() 简历 pattern 指定
在python中,完成对对象的拷贝工作,是利用copy模块和deepcopy模块。
#!/usr/bin/env python # _*_ coding utf-8 _*_ #Author: aaron from copy import copy, deepcopy # 原型抽象类 class Prototype(object): def clone(self): #浅拷贝 pass def deep_clone(self): #深拷贝 pass # 工作经历类 class WorkExperience(object): def __init__(self): self.timearea = ‘‘ self.company = ‘‘ def set_workexperience(self,timearea, company): self.timearea = timearea self.company = company # 简历类 class Resume(Prototype): def __init__(self,name): self.name = name self.workexperience = WorkExperience() def set_personinfo(self, sex, age): self.sex = sex self.age = age pass def set_workexperience(self,timearea, company): self.workexperience.set_workexperience(timearea, company) def display(self): print(self.name) print(self.sex, self.age) print(‘工作经历‘,self.workexperience.timearea, self.workexperience.company) def clone(self): return copy(self) def deep_clone(self): return deepcopy(self) if __name__ == ‘__main__‘: obj1 = Resume(‘andy‘) obj2 = obj1.clone() # 浅拷贝对象 obj3 = obj1.deep_clone() # 深拷贝对象 obj1.set_personinfo(‘男‘,28) obj1.set_workexperience(‘2010-2015‘,‘AA‘) obj2.set_personinfo(‘男‘,27) obj2.set_workexperience(‘2011-2017‘,‘AA‘) # 修改浅拷贝的对象工作经历 obj3.set_personinfo(‘男‘,29) obj3.set_workexperience(‘2016-2017‘,‘AA‘) # 修改深拷贝的对象的工作经历 obj1.display() obj2.display() obj3.display()
男 28 工作经历 2011-2017 AA andy 男 27 工作经历 2011-2017 AA andy 男 29 工作经历 2016-2017 AA
标签:bin print 需要 可变对象 抽象 clone() 简历 pattern 指定
原文地址:https://www.cnblogs.com/cjaaron/p/9136481.html