[Python] 纯文本查看 复制代码 import copy
class MobilePhone:
def __init__(self,cpu,screen):
self.cpu = cpu
self.screen = screen
class CPU:
def calculate(self):
print("计算器")
print("CPU对象:",self)
class Screen:
def show(self):
print("展示画面")
print("Screen对象:",self)
a = CPU()
b = Screen()
#测试浅拷贝
m1 = MobilePhone(a,b)
m2 = copy.copy(m1)
print(m1,m1.cpu,m1.screen)
print(m2,m2.cpu,m2.screen)
#测试深拷贝
m3 = copy.deepcopy(m1)
print(m1,m1.cpu,m1.screen)
print(m3,m3.cpu,m3.screen)
运行结果:<__main__.MobilePhone object at 0x0000017FBEC6EF70> <__main__.CPU object at 0x0000017FBEC6EFD0> <__main__.Screen object at 0x0000017FBEC6EFA0>
<__main__.MobilePhone object at 0x0000017FBEC6ECA0> <__main__.CPU object at 0x0000017FBEC6EFD0> <__main__.Screen object at 0x0000017FBEC6EFA0>
<__main__.MobilePhone object at 0x0000017FBEC6EF70> <__main__.CPU object at 0x0000017FBEC6EFD0> <__main__.Screen object at 0x0000017FBEC6EFA0>
<__main__.MobilePhone object at 0x0000017FBEC6E430> <__main__.CPU object at 0x0000017FBEC70C70> <__main__.Screen object at 0x0000017FBEC70CA0>
关于这以上代码,是测试对象的浅拷贝和深拷贝的,突然就不懂了,m1的实参a,b是不是就为了打印子对象的内存地址?那这样的话类CPU和Screen中的执行语句不就是白写了? |