章节作业
-
图文分析内存
-
设计一个名为MyRectangle 的矩形类来表示矩形
import turtle as t
class MyRectangle:
'''矩形类,求面积、周长,使用海龟绘图绘制矩形'''
def __init__(self,x=0.0,y=0.0,width=100,height=100):
#输入4个值,x,y默认为0,width、height默认为100
self.x = x
self.y = y
self.width = width
self.height = height
def GetArea(self):
#矩形周长 S =ab
if self.x==0 and self.y ==0:
return '同一个点不为矩形'
else:
Area = self.x * self.y
return '{0},{1}的面积是:{2}'.format(self.x,self.y,Area)
def GetPerimeter(self):
#矩形周长 S = (a+b)*2
if self.x ==0 and self.y == 0:
return '同一个点不为矩形'
else:
Perimeter = (self.x+self.y)*2
return '{0},{1}的周长是:{2}'.format(self.x,self.y,Perimeter)
def Draw(self):
#turtle.goto(2个参数 x,y)
t.speed(1)
t.penup()
t.goto(self.x,self.y)
t.pendown()
t.right(90)
t.goto(self.x,self.y+self.height)
t.right(90)
t.goto(self.x-self.width,self.y+self.height)
t.right(90)
t.goto(self.x-self.width,self.y)
t.right(90)
t.goto(self.x,self.y)
t.done()
rectangle = MyRectangle(10,20)
#print(rectangle.GetArea())
#print(rectangle.GetPerimeter())
rectangle.Draw()
-
浅拷贝、深拷贝内存示意图
-
使用组合关系定义汽车类
class Motor:
def work(self):
return 'Motor work....'
class Chassis:
def work(self):
return 'Chassis work....'
class Seat:
def work(self):
return 'Seat work....'
class Shell:
def work(self):
return 'Shell work....'
class Car:
def __init__(self,m,c,s1,s2):
self.m = m
self.c = c
self.s1 = s1
self.s2 = s2
def run(self):
print(self.m.work())
print(self.c.work())
print(self.s1.work())
print(self.s2.work())
m = Motor()
c = Chassis()
s1 = Seat()
s2 = Shell()
car = Car(m,c,s1,s2)
print(car.run())
- 使用工厂模式、单例模式实现需求
class ComputerFactory:
__obj = 0
__init_flag = True
def create(self,brand):
if brand == '联想':
return Lenovo()
elif brand == '华硕':
return Huashuo()
elif brand == '神舟':
return Shenzhou()
else:
return '制造商之外不接单'
def __new__(cls, *args, **kwargs):
if cls.__obj == 0:
cls.__obj = object.__new__(cls)
return cls.__obj
def __init__(self):
if ComputerFactory.__init_flag:
print('ComputerFactory init......')
ComputerFactory.__init_flag = False
class Computer:
def calculate(self):
return 'Computer calculate......'
class Lenovo(Computer):
def callable(self):
return 'Lenovo calculate.......'
class Huashuo(Computer):
def calculate(self):
return 'Huashuo calculate.......'
class Shenzhou(Computer):
def calculate(self):
return 'Shenzhou calculate......'
cf = ComputerFactory()
print(cf.create('联想'))
print(cf.create('华硕'))
print(cf.create('神舟'))
print(cf.create('MAC'))
print('-----------------------')
cf2 = ComputerFactory()
print(cf2.create('联想'))
- 定义雇员类
class Employee:
uid = 1001
def __init__(self, name, salary):
self.name = name
self.__salary = salary
self.uid = self.uid #将当前uid赋值给创建对象
Employee.uid += 1 #再次新建+1
@property
def salary(self):
return self.__salary
@salary.setter
def salary(self, salary):
if 1000 < salary < 50000:
self.__salary = salary
return self.__salary
else:
print('无效数值不予设置')
def __add__(self, other):
if isinstance(other, Employee): # isinstance(其他变量,是否属于同类型[此项填同个类Employee])
return self.__salary + other.__salary
else:
return '不是同类型无法相加'
e1 = Employee('qu', 30000)
e2 = Employee('pu', 40000)
print(e1.uid, e1.name, e1.salary)
print(e2.uid, e2.name, e2.salary)
x = e1 + e2
print(x) # 返回薪水和
print('--------------------')
e1.salary = -5000 # 在具体条件内设置薪水否则报错并返回原值
print(e1.salary)
print('--------------------')