python学习
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(10,5)
p.display()
c = Circle(10)
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()
标签:perimeter,return,area,self,博客,cal,4.24,def From: https://www.cnblogs.com/zjm921/p/18250466