(一)实验指导书-23:自定义类模拟三维向量及其运算
实验目的:
1、了解如何定义一个类。
2、了解如何定义类的私有数据成员和成员方法。
3、了解如何使用自定义类实例化对象。
实验内容:定义一个三维向量类,并定义相应的特殊方法实现两个该类对象之间的加、减运算(要求支持运算符+、-),实现该类对象与标量的乘、除运算(要求支持运算符*、/),以及向量长度的计算(要求使用属性实现)。
class Vector_3:
# 构造方法,初始化,定义向量坐标
def __init__(self, x, y, z):
self._x = x
self._y = y
self._z = z
# 向量加法:对应分量相加,返回新向量
def __add__(self, anotherPoint):
x = self._x+anotherPoint._x
y = self._y+anotherPoint._y
z = self._z+anotherPoint._z
return Vector_3(x, y, z)
# 向量减法:对应分量相减,返回新向量
def __sub__(self, anotherPoint):
x = self._x-anotherPoint._x
y = self._y-anotherPoint._y
z = self._z-anotherPoint._z
return Vector_3(x, y, z)
# 向量乘法:各分量乘以同一个数字,返回新向量
def __mul__(self, n):
x, y, z = self._x*n, self._y*n, self._z*n
return Vector_3(x, y, z)
# 向量除法:各分量除以同一个数字,返回新向量
def __truediv__(self, n):
x, y, z = self._x/n, self._y/n, self._z/n
return Vector_3(x, y, z)
# 向量长度:计算所有分量平方和的平方根
@property
def length(self):
return (self._x**2+self._y**2+self._y**2)**0.5
def __str__(self):
return 'Vector_3({},{},{})'.format(self._x, self._y, self._z)
v1 = Vector_3(1, 3, 5)
v2 = Vector_3(2, 4, 6)
print(v1+v2)
print(v1-v2)
print(v1*3)
print(v2/2)
print(v1.length)
(二)定义一个Person父类和Student, Staff子类;父类定义shouru,display方法,Student类的收入按照初始化奖学金计算,Staff的按照60%*工资+40%*奖金计算,调用display显示工资。编写测试代码。
# 定义父类Person
class Person:
#构造函数
def __init__(self,name,income):
self._name=name
self._income=income
def display(self):
print('Person',self._name,'的收入是:',self._income)
#定义学生类
class Student(Person):
# 构造函数
def __init__(self,name,scholarship):
self._name=name
self._scholarship=scholarship
def display(self):
print('学生',self._name,'的奖学金是:',self._scholarship)
#定义职工类
class Staff(Person):
# 构造函数
def __init__(self,name,wages,bonus):
self._name=name
self._wages=wages
self._bonus=bonus
def display(self):
print('职工',self._name,'的收入是:',self._wages*0.6+self._bonus*0.4)
# 实例化对象并调用函数
person=Person('kkk',5000)
person.display()
student=Student('lll',3000)
student.display()
staff=Staff('jjj',6000,4000)
staff.display()
(三)实验指导书-24:自定义类实现带超时功能的队列结构,并测试。
实验目的:
1、了解标准库 time 中 time()函数的用法。
2、了解如何定义一个类。
3、理解队列结构的特点。
4、理解入队和出队时超时功能的实现。
实验内容: 编写程序,实现自定义类,模拟队列结构。要求实现入队、出队以及修改队列大小和判断队列是否为空、是否为满的功能,同时要求在入队时如果队列已满则等待指定时间、出队时如果队列已空则等待指定时间等辅助功能。
import time
class Queue:
# 构造函数
def __init__(self, size=10):
self._content = []
self._size = size
self._current = 0
# 设置队列的长度
def setSize(self, size):
if size < self._current:
# 如果队列缩小,删除后面的元素
for i in range(size, self._current)[::-1]:
del self._content[i]
self._current = size
self._size = size #更新队列长度
# 模拟入队操作
def enter_queue(self,v,timeout=999999):
for i in range(timeout):
# 当队列未满时,在队尾追加元素
if self._current<self.size:
self._content.append(v)
self._current=self._current+1
break
time.sleep(1)
# 当队列已满,为阻塞状态,超时放弃
else:
return '队列已满,超时放弃'
# 模拟出队操作
def leave_queue(self,timeout=999999):
for i in range(timeout):
# 从列表头部弹出元素
if self._content:
self._current-=1
return self._content.pop(0)
time.sleep(1)
# 当队列为空,为阻塞状态,超时放弃
else:
return '队列为空,超时放弃'
def show(self):
# 当列表非空,输出列表
if self._content:
print(self._content)
else:
print('队列为空')
#清空队列
def empty(self):
self._content=[]
self._current=0
# 判断队列是否为空
def wheather_Empty(self):
return not self._content
# 判断队列是否已满
def wheather_Full(self):
return self._current==self._size
if __name__ =='___main___':
pass