4.18
所花时间:4小时
代码量:88
博客篇:1
python
设计高校人员信息包,并完成测试
【题目描述】定义一个人员类People,其属性有:姓名、性别、年龄;基于People实现学生类Student,添加属性:学号、入学时间和入学成绩;基于People实现教师类Teacher,添加属性:职务、部门、工作时间;基于Student实现究生类Graduate,添加属性:研究方向和导师,分别定义其中的构造函数和输出函数。程序中定义各种类的对象,并完成测试。 基于以上类利用接口实现在职研究生类 GradOnWork。
【源代码程序】
class People : def __init__(self, name = 'WangJianMin',gender = '男',age=45): self.name = name self.gender = gender self.age = age def display(self): print("姓名:"+self.name+"\n性别:"+self.gender+"\n年龄:"+str(self.age)) class Student(People): def __init__(self,student_id="20223250",into_time="2022-09-01",into_score="100.0"): People.__init__(self,name = 'ZhangSan',gender = '男',age=20) self.student_id = student_id self.into_time = into_time self.into_score = into_score def display(self): print("姓名:"+self.name+"\n性别:"+self.gender+"\n年龄:"+str(self.age)+"\n学号:"+self.student_id+"\n入学时间:"+self.into_time+"\n入学成绩:"+str(self.into_score)) class Teacher(People): def __init__(self,post="院长",department="软件工程学院",job_time = "2008-12-03"): People.__init__(self, name = 'WangJianMin',gender = '男',age=45) self.post = post self.department = department self.job_time = job_time def display(self): print("姓名:"+self.name+"\n性别:"+self.gender+"\n年龄:"+str(self.age)+"\n职务:"+self.post+"\n部门:"+self.department+"\n工作时间:"+self.job_time) class Graduate(Student): def __init__(self,research_fields="人工智能",teacher = "Mr.Liu"): Student.__init__(self,student_id="20193250",into_time="2019-09-01",into_score="100.0") self.research_fields = research_fields self.teacher = teacher def display(self): print("姓名:"+self.name+"\n性别:"+self.gender+"\n年龄:"+str(self.age)+"\n学号:"+self.student_id+"\n入学时间:"+self.into_time+"\n入学成绩:"+str(self.into_score)+"\n研究方向:"+self.research_fields+"\n导师:"+self.teacher) class GradOnWork(Teacher,Graduate): def __init__(self): Teacher.__init__(self,post="辅导员",department="软件工程学院",job_time = "2008-12-03") Graduate.__init__(self,research_fields="人工智能",teacher = "Mr.Liu") def display(self): print("姓名:"+self.name+"\n性别:"+self.gender+"\n年龄:"+str(self.age)+"\n职务:"+self.post+"\n部门:"+self.department+"\n工作时间:"+self.job_time+"\n学号:"+self.student_id+"\n入学时间:"+self.into_time+"\n入学成绩:"+str(self.into_score)+"\n研究方向:"+self.research_fields+"\n导师:"+self.teacher) p = People() # 人员类 p.display() s = Student() # 学生类 s.display() t = Teacher() # 教师类 t.display() g = Graduate() # 研究生类 g.display() gw = GradOnWork() # 在职研究生类 gw.display()
以圆类为基础设计三维图形体系
【题目描述】设计三维图形类体系,要求如下:
设计三维图形功能接口,接口包含周长、面积、体积计算方法;
基于以上接口,首先定义点类,应包含x,y坐标数据成员,坐标获取及设置方法、显示方法等;
以点类为基类派生圆类,增加表示半径的数据成员,半径获取及设置方法,重载显示函数,并可计算周长和面积等;
以圆类为基础派生球类、圆柱类、圆锥类;要求派生类球、圆柱、圆锥中都含有输入和输出显示方法;并可计算面积、周长。
程序中定义各种类的对象,并完成测试。
【源代码程序】
import math class Shape: def cal_perimeter(self): pass def cal_area(self): pass def cal_volume(self): pass class Point(Shape): def __init__(self,x=0,y=0): self.x = x self.y = y def setX(self,x): self.x = x def setY(self,y): self.y = y def getX(self): return self.x def getY(self): return self.y def display(self): print('({}'.format(self.x)+',{})'.format(self.y)) class Circle(Point): def __init__(self,r): Point.__init__(self,x=0,y=0) self.r = r def setR(self,r): self.r = r def getR(self): return self.r def cal_perimeter(self): perimeter=round(2*3.14*self.r,2) return perimeter def cal_area(self): area=round(3.14*self.r**2,2) return area def display(self): print('圆的周长:',self.cal_perimeter()) print('圆的面积:',self.cal_area()) class Ball(Circle): def __init__(self,r): Circle.__init__(self,r=10) def cal_volume(self): perimeter=round((4/3)*3.14*(self.r**3),2) return perimeter def cal_area(self): area=round(3.14*self.r**2*4,2) return area def display(self): print('球的体积:',self.cal_volume()) print('球的面积:',self.cal_area()) class Circular(Circle): def __init__(self,r,h): Circle.__init__(self,r=10) self.h = h def cal_volume(self): perimeter=round(3.14*(self.r**2)*self.h,2) return perimeter def cal_area(self): area=round(3.14*2*self.r*self.h+2*3.14*self.r**2,2) return area def display(self): print('圆柱的体积:',self.cal_volume()) print('圆柱的面积:',self.cal_area()) class CircularCone(Circle): def __init__(self,r,h): Circle.__init__(self,r=10) self.h = h def cal_volume(self): perimeter=round((1/3)*3.14*(self.r**2)*self.h,2) return perimeter def cal_area(self): area=round(3.14*self.r*(self.r+math.sqrt(self.r**2+self.h**2)),2) return area def display(self): print('圆锥的体积:',self.cal_volume()) print('圆锥的面积:',self.cal_area()) p = Point(6,2) p.display() c = Circle(4) c.display() r = float(input("请输入球体半径:")) b = Ball(r) b.display() r = float(input("请输入圆柱底面半径:")) h = float(input("请输入圆柱的高:")) cc = Circular(r,h) cc.display() r = float(input("请输入圆锥底面半径:")) h = float(input("请输入圆锥的高:")) cn = CircularCone(r,h) cn.display()