1、定义并实现一个矩形类Rectangle,其私有实例成员为矩形的左下角与右上角两个点的坐标,能设置左下角和右上角两个点的位置,能根据左下角与右上角两个点的坐标计算矩形的长、宽、周长和面积,另外根据需要适当添加其他成员方法和特殊方法(如构造方法)。实现并测试这个类。
class Rectangle:
def __init__(self,New_zxx,New_zxy,New_ysx,New_ysy):
self.zxx=New_zxx
self.zxy=New_zxy
self.ysx=New_ysx
self.ysy=New_ysy
self.length = self.ysx - self.zxx
self.width = self.ysy - self.zxy
def perimeter(self):
print('周长:{}'.format(2*(self.length+self.width)))
def area(self):
print('面积为:{}'.format(self.length*self.width))
a = Rectangle(0,0,1,2)
print(a.length)
print(a.width)
a.perimeter()
a.area()
2、创建SchoolMem类,该类中包含三个数据成员:姓名、性别、年龄;创建Student类,继承自SchoolMem类,添加额外三个数据成员:班级、学号和数量统计;创建Teacher类,继承自SchoolMem类,添加额外三个数据成员:学院、工号和数量统计。要求给每个类适当添加属性、成员方法和特殊方法(如构造方法),实现学生和教师信息的输入输出。实现并测试这个类。(建议在父类和子类中使用同名函数以实现多态,如在SchoolMem类、Student类和Teacher类中分别实现printInfo方法输出对象的信息。)
class SchoolMem:
def __init__(self,name,sex,age):
self.name=name
self.sex = sex
self.age = age
def Alter(self):
self.name = input('姓名')
self.sex = input('性别')
self.age = input('年龄')
def PrintInfo(self):
print(self.name)
print(self.sex)
print(self.age)
class Student(SchoolMem):
total = 0
def __init__(self,name,sex,age,cno,sno):
super().__init__(name,sex,age)
self.cno = cno
self.sno = sno
Student.total+=1
def Alter(self):
self.cno = input('输入学生所属班级:')
self.sno = input('输入学生学号:')
def PrintInfo(self):
super(Student, self).PrintInfo()
print(self.cno)
print(self.sno)
class Teacher(SchoolMem):
total = 0
def __init__(self,name,sex,age,cono,jno):
super().__init__(name, sex, age)
self.cono = cono
self.jno = jno
Teacher.total +=1
def Alter(self):
self.cono = input('输入老师所属学院:')
self.jno = input('输入老师工号:')
def PrintInfo(self):
super(Teacher, self).PrintInfo()
print(self.cono)
print(self.jno)
a=Student('张三','男','18',5,1902073564)
a.PrintInfo()
b=Teacher('李四','男','30',2,1351332)
b.PrintInfo()
3、给如下自定义列表类Mylist添加运算符重载函数,实现自定义列表的加法和乘法运算
class Mylist:
def __init__(self,iterable=()):
self.data = list(iterable)
让如下代码能正确运行:
L1 = MyList([1,2,3])
L2 = MyList([4,5,6])
L3 = L1+ L2
print(L3) #输出MyList([1,2,3,4,5,6])
L4 = L2 + L1
print(L4) #输出MyList([4,5,6,1,2,3])
L5 = L1 * 3
print(L5) #输出MyList([1,2,3,1,2,3,1,2,3])
L6 = 3 * L1
print(L6) #输出MyList([1,2,3,1,2,3,1,2,3])
L1 += L2
print(L1) #输出MyList([1,2,3,4,5,6])
L7 = -L1
print(L7) #输出MyList([-1,-2,-3,-4,-5,-6])
class MyList:
def __init__(self,iterable=()):
self.data = list(iterable)
def __add__(self, other):
a = MyList(self.data + other.data)
return a
def __mul__(self, n):
b = MyList(self.data * 3)
return b
def __rmul__(self, n):
c = MyList(3*self.data)
return c
def __iadd__(self, other):
self.data += other.data
return self
def __neg__(self):
e = MyList()
for i in self.data:
e.data.append(i*-1)
return e
def __repr__(self):
return repr('MyList({})'.format(self.data))
L1 = MyList([1,2,3])
L2 = MyList([4,5,6])
L3 = L1 + L2
print(L3) #输出MyList([1,2,3,4,5,6])
L4 = L2 + L1
print(L4) #输出MyList([4,5,6,1,2,3])
L5 = L1 * 3
print(L5) #输出MyList([1,2,3,1,2,3,1,2,3])
L6 = 3 * L1
print(L6)
L1 += L2
print(L1)
L7 = -L1
print(L7) #输出MyList([-1,-2,-3,-4,-5,-6])
4、设计一个用于学生管理的类Student(学生)。考虑到通用性,这里只抽象出所有学生都具有的数据成员:学号、姓名、性别、年龄、班级等。要求给每个数据成员声明对应的读写属性,定义下列成员方法:学生信息的录入、显示以及修改,另外根据需要适当添加特殊方法(如构造方法)。实现并测试这个类。
class Student:
def __init__(self,sno='',name='',sex='',age=0,classes=0):
self.__sno = sno
self.__name = name
self.__sex = sex
self.__age = age
self.__classes = classes
def __get(self):
return self.__sno
def __set(self,a):
self.__sno = a
sno = property(__get,__set)
def __get__(self):
return self.__name
def __set(self,a):
self.__name = a
name = property(__get,__set)
def __get(self):
return self.__sex
def __set(self,a):
self.__sex = a
sex = property(__get,__set)
def __get(self):
return self.__age
def __set(self,a):
self.__age = a
age = property(__get,__set)
def __get(self):
return self.__classes
def __set(self,a):
self.__classes = a
classes = property(__get,__set)
def InputInfo(self):
print('输入学生信息')
sno = input('学号:')
if not (len(sno)==10):
print('学号必须是十位')
return None
name = input('姓名:')
sex = input('性别(男,女)')
if sex not in ('男','女'):
print('性别必须是男或女')
return None
age = eval(input('年龄:'))
if not isinstance(age,int):
print('年龄必须为整数')
return None
classes = input('班级:')
self.__sno = sno
self.__name = name
self.__sex = sex
self.__age = age
self.__classes = classes
return self
def show(self):
print(f'sno:{self.__sno}\nname:{self.__name}\nsex:{self.__sex}\nage:{self.__age}\nclasses:{self.__classes}')
def alter(self):
while True:
print('选择您要更改的信息:')
print('1:修改学号')
print('2:修改姓名')
print('3:修改性别')
print('4:修改年龄')
print('5:修改班级')
print('0:退出修改')
i = eval(input('输入选项:'))
if i == 0:
self.show()
break
elif i==1:
sno = input('输入学号:')
if not len(sno)==10:
print('学号为10位')
continue
self.__sno = sno
elif i == 2:
name = input('输入姓名:')
self.__name = name
elif i == 3:
sex = input('性别(男,女)')
if sex not in ('男', '女'):
print('性别必须是男或女')
continue
self.__sex = sex
elif i == 4:
age = eval(input('年龄:'))
if not isinstance(age, int):
print('年龄必须为整数')
continue
self.__age = age
elif i == 5:
self.__classes = input('班级:')
a = p6t4.Student()
a.InputInfo()
输入学生信息:
学号:>? 1901070217
姓名:>? gzk
性别(男,女)>? 男
年龄:>? 15
班级:>? 2
<p6t4.Student object at 0x00000211D488A288>
a.show()
sno:1901070217
name:gzk
sex:男
age:15
classes:2
a.alter()
选择您要更改的信息:
1:修改学号
2:修改姓名
3:修改性别
4:修改年龄
5:修改班级
0:退出修改
输入选项:>? 2
输入姓名:>? 郭zk
选择您要更改的信息:
1:修改学号
2:修改姓名
3:修改性别
4:修改年龄
5:修改班级
0:退出修改
输入选项:>? 0
sno:1901070217
name:郭zk
sex:男
age:15
classes:2
5、在第4题的基础上定义并实现一个学生数组类StudentArray,支持一批学生信息的处理,能实现以下菜单功能:
(1)学生信息录入
(2)学生信息显示(显示所有学生信息)
(3)学生信息删除(按学号)
class StudentArray:
__size = 0
def __init__(self):
self.__value = []
def InputInfo(self):
a = Student()
a = a.InputInfo()
if a != None:
self.__value.append(a)
print('录入成功')
StudentArray.__size += 1
def search(self):
sno = input('请输入查询学号:')
for i in range(len(self.__value)):
if sno == self.__value[i].sno:
return i
else:
print('不存在此学号的学生')
return -1
def show(self):
print('所有学生信息为:')
for a in self.__value:
a.show()
print('测试')
def delete(self):
i = self.search()
if i != -1:
del self.__value[i]
StudentArray.__size -= 1
print('删除成功')
else:
print('删除失败')
def menu():
print('''
1.学生信息录入
2.学生信息显示
3.学生信息删除
4.退出系统''')
def main():
b = StudentArray()
while True:
menu()
n = eval(input('请输入选择:'))
if n == 1:
b.InputInfo()
print()
elif n == 2:
b.show()
print()
elif n == 3:
b.delete()
print()
elif n ==4:
print('已经退出系统')
break
else:
print('输入错误,重新输入')
print()
if __name__ == '__main__':
main()