第一题:
浅拷贝
Python拷贝一般都是浅拷贝。拷贝时,对象包含的子对象内容不拷贝。因此,源对象 和拷贝对象会引用同一个子对象。
深拷贝
使用copy模块的 deepcopy 函数,递归拷贝对象中包含的子对象。源对象和拷贝对象 所有的子对象也不同。
测试对象的浅拷贝和深拷贝
class MobilePhone:
def init(self, cpu, screen):
self.cpu = cpu
self.screen = screen
class CPU:
def calculate(self): # 这个单词是 计算的意思
print(“算你个12345”)
print(“cpu对象:”, self)
class Screen:
def show(self):
print(“显示一个好看的画面”)
print(“screen对象:”, self)
测试变量赋值
import copy
c1 = CPU()
c2 = c1
print(c1)
print(c2)
print(“测试浅复制…”)
测试浅复制
In [2]: c1 = CPU()
…: c2 = c1
In [3]:
In [3]:
In [3]: c1
Out[3]: <main.CPU at 0x59313d0>
In [4]: c2
Out[4]: <main.CPU at 0x59313d0>
In [6]: # c1 和 c2 是两个不同的对象
In [7]: # 开始测试浅拷贝
In [8]: s1 = Screen()
…: m1 = MobilePhone(c1, s1)
…: m2 = copy.copy(m1)
In [9]: s1
Out[9]: <main.Screen at 0x5ffb850>
In [11]: m1
Out[11]: <main.MobilePhone at 0x5ffb6b0>
In [12]: m2
Out[12]: <main.MobilePhone at 0x5ffb3b0>
证明过程
In [13]: print(f"mobilephone -> {m1} | ‘cpu’ -> {m1.cpu} |‘screen’ ->
…: {m1.screen}")
mobilephone -> <main.MobilePhone object at 0x05FFB6B0> | ‘cpu’ -> <main.CPU object at 0x059313D0> |‘screen’ -> <main.Screen object at 0x05FFB850>
In [14]: print(f"mobilephone -> {m2} | ‘cpu’ -> {m2.cpu} |‘screen’ -> {m2.screen}")
mobilephone -> <main.MobilePhone object at 0x05FFB3B0> | ‘cpu’ -> <main.CPU object at 0x059313D0> |‘screen’ -> <main.Screen object at 0x05FFB850>
测试深复制
print(“测试深复制…”)
m3 = copy.deepcopy(m1)
print(m1, m1.cpu, m1.screen)
print(m3, m3.cpu, m3.screen)
这次可以看到 不但 手机不一样。cpu 和 屏幕 也是不一样的 对象。
这就是 深拷贝
In [15]: m3 = copy.deepcopy(m1)
…: print(m1, m1.cpu, m1.screen)
…: print(m3, m3.cpu, m3.screen)
<main.MobilePhone object at 0x05FFB6B0> <main.CPU object at 0x059313D0> <main.Screen object at 0x05FFB850>
<main.MobilePhone object at 0x012505F0> <main.CPU object at 0x01250CB0> <__mai
第二题:
class Car():
def init(self,motor,chassis,seat,shell):
self.motor=motor
self.chassis=chassis
self.seat=seat
self.shell=shell
def run(self):
self.motor.work()
self.seat.work()
self.chassis.work()
class motor:
def work(self):
print(“调用发动机”)
class chassis:
def work(self):
print(“调用地盘”)
class seat:
def work(self):
print(“调用座椅”)
class shell:
def work(self):
print(“调用外壳”)
m=motor()
c=chassis()
se=seat()
sh=shell()
car=Car(m,c,se,sh)
car.run()
此段代码摘抄借鉴于https://blog.csdn.net/weixin_43306936/article/details/114378225
第三题:
class ComputerFactory:
_obj = None
_init_flag = True
def create_computer(self, brand):
if brand == "联想":
return Lenovo()
elif brand == "华硕":
return Asus()
elif brand == "神舟":
return Hasee()
else:
return "未知品牌"
def __new__(cls, *args, **kwargs):
if cls._obj == None:
cls._obj = object.__new__(cls)
return cls._obj
def __init__(self):
if ComputerFactory._init_flag:
print("init ComputerFactory")
ComputerFactory._init_flag = False
class Computer:
def init(self):
pass
def calculate(self):
print(“计算出数据”)
class Lenovo:
def __init__(self):
Computer.__init__(self)
def calculate(self):
print("我是联想")
class Asus:
def init(self):
Computer.init(self)
def calculate(self):
print("我是华硕")
class Hasee:
def init(self):
Computer.init(self)
def calculate(self):
print("我是神舟")
com1 = ComputerFactory()
com2 = ComputerFactory()
print(com1)
print(com2)
h = com1.create_computer(“华硕”)
h.calculate()
#此段代码借鉴于:https://blog.csdn.net/Beauty_Lion/article/details/117884787
第四题:
class Employee:
id = 1001
def __init__(self, name, salary):
self.name = name
self.id = Employee.id
Employee.id = Employee.id + 1
self._salary = salary
def __add__(self, other):
if isinstance(other, Employee):
return self._salary + other._salary
@property
def salary(self):
print("薪水为{0}".format(self._salary))
return self._salary
@salary.setter
def salary(self,salary):
if (1000 < salary <50000):
self._salary = salary
else:
print("薪水错误")
E1 = Employee(“张三”,5000)
E2 = Employee(“李四”,8800)
print(E1.id, E2.id)
print(E1 + E2)
E1.salary = 9000
print(E1.salary)
#此段代码借鉴于:https://blog.csdn.net/Beauty_Lion/article/details/117884787