今天写了一些python的实验
定义一个人员类People,其属性有:姓名、性别、年龄;基于People实现学生类Student,添加属性:学号、入学时间和入学成绩;基于People实现教师类Teacher,添加属性:职务、部门、工作时间;基于Student实现究生类Graduate,添加属性:研究方向和导师,分别定义其中的构造函数和输出函数。程序中定义各种类的对象,并完成测试。 基于以上类利用接口实现在职研究生类 GradOnWork。
class People: def __init__(self, name='粘**', gender='男', age=21): 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="20214145", into_time="2021-09-01", into_score="100"): People.__init__(self, name='左**', gender='男', age=21) 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="2000-1-01"): People.__init__(self, name='王**', gender='男', age=40) 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="王**"): Student.__init__(self, student_id="20211111", into_time="2021-09-01", into_score="100") 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="2000-1-01") Graduate.__init__(self, research_fields="智能算法", teacher="罗**") 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 C(self):#计算圆的周长 perimeter=round(2*3.14*self.r,2) return perimeter def S(self):#计算圆的面积 area=round(3.14*self.r**2,2) return area def display(self): print('圆的周长:',self.C()) print('圆的面积:',self.S()) class Ball(Circle): def __init__(self,r): Circle.__init__(self,r=10) self.r = r def V(self):#计算球的体积 perimeter=round((4/3)*3.14*(self.r**3),2) return perimeter def S(self):#计算球的面积 area=round(3.14*self.r**2*4,2) return area def display(self): print('球的体积:',self.V()) print('球的面积:',self.S()) class Circular(Circle): def __init__(self,r,h): Circle.__init__(self,r=10) self.r = r self.h = h def V(self):#计算圆柱的体积 perimeter=round(3.14*(self.r**2)*self.h,2) return perimeter def S(self):#计算圆柱的面积 area=round(3.14*2*self.r*self.h+2*3.14*self.r**2,2) return area def display(self): print('圆柱的体积:',self.V()) print('圆柱的面积:',self.S()) class CircularCone(Circle): def __init__(self,r,h): Circle.__init__(self,r=10) self.r = r self.h = h def V(self):#计算圆锥的体积 perimeter=round((1/3)*3.14*(self.r**2)*self.h,2) return perimeter def S(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.V()) print('圆锥的面积:',self.S()) p = Point(3,3) p.display() r = float(input("请输入圆的半径:")) c = Circle(r) 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()
标签:__,self,init,五月,print,五日,display,def From: https://www.cnblogs.com/mine-my/p/17375467.html