python练习1
python习题1
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()
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()