Python实验九 面向对象程序设计方法
1.写出程序的运行结果
class P1:def foo(self):print('callable P1-foo')
class P2:def foo(self):print('callable P2-foo')def bar(self):print('callable P2-bar')
class C1(P1,P2):pass
class C2(P1,P2):def bar(self):print('callable C2-bar()')
class GC(C1,C2):pass
gc=GC()
gc.foo()
gc.bar()
3.写出程序的运行结果
import math
class Circle:def __init__(self, radius):self.radius=radiusdef getRadius(self):return self.radiusdef getArea(self):return math.pi*self.radius*self.radius
def main():c1=Circle(10)print(c1.getRadius())print('{:7.2f}'.format(c1.getArea()))
if __name__=='__main__':main()
4.定义一个 Circle 类,根据圆的半径求周长和面积。再由 Circle 类创建两个 圆对象,其半径分别为 5 和 10,要求输出各自的周长和面积。请补充程序
import math
class Circle:def __init__(self,radius=5): #完成构造函数self.radius=radiusdef getPerimeter(self):return 2*math.pi*self.radiusdef getArea(self):return math.pi*self.radius*self.radius
c1=Circle()
c2=Circle(10) #向构造函数传递新的参数
print(c1.radius, c1.getPerimeter(), c1.getArea())
print(c2.radius, c2.getPerimeter(), c2.getArea())
4.利用面向对象方法求 n!,并输出 10!的值
class Factorial:def fac (self,num):factorial = 1for i in range(1,num+1):factorial *= ireturn factorialif __name__ == '__main__':factorial = Factorial()n=int(input('请输入n:'))print(n,'!=',factorial.fac(n))
5.定义 change 类,用以实现角度和弧度之间的转换
class Change:import mathdef rad_to_ang(self,radian):return radian*180/math.pidef ang_to_rad(self,angle):return angle*math.pi/180if __name__ == '__main__':import mathchange=Change()print('{0}弧度 = {1}°'.format(math.pi,change.rad_to_ang(math.pi)))print('{0}° = {1}弧度'.format(90,change.ang_to_rad(90)))
6.设计点类,并为这个点类设置一个方法来计算两点之间的距离
class Point:def cal_distance (self,p1,p2):import mathreturn math.sqrt(math.pow((p2[0] - p1[0]), 2) + math.pow((p2[1] - p1[1]), 2))if __name__ == '__main__':point=Point()p1=[0,0]p2=[1,1]print('点{0}与点{1}之间的距离是{2}'.format(p1,p2,point.cal_distance(p1,p2)))
7.设计长方形类,并用其成员函数计算两个给定的长方形的周长和面积
class Rectangle:def get_perimeter(self,length,width):return 2*(length+width)def get_area(self,length,width):return length*widthif __name__ == '__main__':rectangle=Rectangle()print('长{0}宽{1}的长方形:周长={2},面积={3}'.format(3,4,rectangle.get_perimeter(3,4),rectangle.get_area(3,4)))print('长{0}宽{1}的长方形:周长={2},面积={3}'.format(11,11,rectangle.get_perimeter(3,4),rectangle.get_area(3,4)))
8.已有若干个学生数据,这些数据包括学号,姓名,程序设计基础成绩,高等 数学成绩和英语成绩,要求定义学生类,并用其成员函数求各门课程的平均分
class Student:def __init__(self,id,name,program,math,english):self.id=idself.name=nameself.program=programself.math=mathself.english=english@staticmethoddef get_avg(*students):program_total=0math_total=0english_total=0for i in range(len(students)):program_total += students[i].programmath_total += students[i].mathenglish_total += students[i].englishreturn [program_total/len(students),math_total/len(students),english_total/len(students)]if __name__ == '__main__':student1=Student('001','张三',55,55,55)student2=Student('002','李四',66,66,66)student3=Student('003','王五',77,77,77)students=[student1,student2,student3]print('平均分\n程序设计基础:{0},高等数学:{1},英语:{2}'.format(Student.get_avg(*students)[0],Student.get_avg(*students)[1],Student.get_avg(*students)[2]))
Python实验九 面向对象程序设计方法
1.写出程序的运行结果
class P1:def foo(self):print('callable P1-foo')
class P2:def foo(self):print('callable P2-foo')def bar(self):print('callable P2-bar')
class C1(P1,P2):pass
class C2(P1,P2):def bar(self):print('callable C2-bar()')
class GC(C1,C2):pass
gc=GC()
gc.foo()
gc.bar()
3.写出程序的运行结果
import math
class Circle:def __init__(self, radius):self.radius=radiusdef getRadius(self):return self.radiusdef getArea(self):return math.pi*self.radius*self.radius
def main():c1=Circle(10)print(c1.getRadius())print('{:7.2f}'.format(c1.getArea()))
if __name__=='__main__':main()
4.定义一个 Circle 类,根据圆的半径求周长和面积。再由 Circle 类创建两个 圆对象,其半径分别为 5 和 10,要求输出各自的周长和面积。请补充程序
import math
class Circle:def __init__(self,radius=5): #完成构造函数self.radius=radiusdef getPerimeter(self):return 2*math.pi*self.radiusdef getArea(self):return math.pi*self.radius*self.radius
c1=Circle()
c2=Circle(10) #向构造函数传递新的参数
print(c1.radius, c1.getPerimeter(), c1.getArea())
print(c2.radius, c2.getPerimeter(), c2.getArea())
4.利用面向对象方法求 n!,并输出 10!的值
class Factorial:def fac (self,num):factorial = 1for i in range(1,num+1):factorial *= ireturn factorialif __name__ == '__main__':factorial = Factorial()n=int(input('请输入n:'))print(n,'!=',factorial.fac(n))
5.定义 change 类,用以实现角度和弧度之间的转换
class Change:import mathdef rad_to_ang(self,radian):return radian*180/math.pidef ang_to_rad(self,angle):return angle*math.pi/180if __name__ == '__main__':import mathchange=Change()print('{0}弧度 = {1}°'.format(math.pi,change.rad_to_ang(math.pi)))print('{0}° = {1}弧度'.format(90,change.ang_to_rad(90)))
6.设计点类,并为这个点类设置一个方法来计算两点之间的距离
class Point:def cal_distance (self,p1,p2):import mathreturn math.sqrt(math.pow((p2[0] - p1[0]), 2) + math.pow((p2[1] - p1[1]), 2))if __name__ == '__main__':point=Point()p1=[0,0]p2=[1,1]print('点{0}与点{1}之间的距离是{2}'.format(p1,p2,point.cal_distance(p1,p2)))
7.设计长方形类,并用其成员函数计算两个给定的长方形的周长和面积
class Rectangle:def get_perimeter(self,length,width):return 2*(length+width)def get_area(self,length,width):return length*widthif __name__ == '__main__':rectangle=Rectangle()print('长{0}宽{1}的长方形:周长={2},面积={3}'.format(3,4,rectangle.get_perimeter(3,4),rectangle.get_area(3,4)))print('长{0}宽{1}的长方形:周长={2},面积={3}'.format(11,11,rectangle.get_perimeter(3,4),rectangle.get_area(3,4)))
8.已有若干个学生数据,这些数据包括学号,姓名,程序设计基础成绩,高等 数学成绩和英语成绩,要求定义学生类,并用其成员函数求各门课程的平均分
class Student:def __init__(self,id,name,program,math,english):self.id=idself.name=nameself.program=programself.math=mathself.english=english@staticmethoddef get_avg(*students):program_total=0math_total=0english_total=0for i in range(len(students)):program_total += students[i].programmath_total += students[i].mathenglish_total += students[i].englishreturn [program_total/len(students),math_total/len(students),english_total/len(students)]if __name__ == '__main__':student1=Student('001','张三',55,55,55)student2=Student('002','李四',66,66,66)student3=Student('003','王五',77,77,77)students=[student1,student2,student3]print('平均分\n程序设计基础:{0},高等数学:{1},英语:{2}'.format(Student.get_avg(*students)[0],Student.get_avg(*students)[1],Student.get_avg(*students)[2]))
发布评论