圆和圆柱体计算(继承)Python

目录

题目描述

题目分析

AC代码


题目描述

定义一个CPoint点类,包含数据成员x,y(坐标点)。

以CPoint为基类,派生出一个圆形类CCircle,增加数据成员r(半径)和一个计算圆面积的成员函数。

再以CCircle做为直接基类,派生出一个圆柱体类CCylinder,增加数据成员h(高)和一个计算体积的成员函数。

生成圆和圆柱体对象,调用成员函数计算面积或体积并输出结果。

输入

输入圆的圆心位置、半径

输入圆柱体圆心位置、半径、高

输出

输出圆的圆心位置 半径

输出圆面积

输出圆柱体的圆心位置 半径 高

输出圆柱体体积

输入样例1 

0 0 1
1 1 2 3

输出样例1

Circle:(0,0),1
Area:3.14
Cylinder:(1,1),2,3
Volume:37.68

题目分析

原本为C++开发的面向对象题目其实并不适合python,python的输入只能以回车结束,一行输入数据只能由一行代码搞定,无法达到输入只读取前若干个,python输入是以字符串读入的,很多问题都可以用字符串解释。

AC代码

class CPoint:
    def getpoint(self):
        self.__x,self.__y=map(int,input().split())
class CCircle(CPoint):
    def getcicle(self):
        self.__x,self.__y,self.__r=map(int,input().split())
    def area(self):
        print('Circle:(%d,%d),%d\nArea:%.2f'%(self.__x,self.__y,self.__r,3.14*self.__r*self.__r))
class CCylinder(CCircle):
    def getcylinder(self):
        self.__x,self.__y,self.__r,self.__h=map(int,input().split())
    def volume(self):
        print('Cylinder:(%d,%d),%d,%d\nVolume:%.2f'%(self.__x,self.__y,self.__r,self.__h,3.14*self.__r*self.__r*self.__h))
circle=CCircle()
circle.getcicle()
circle.area()
cylinder=CCylinder()
cylinder.getcylinder()
cylinder.volume()

猜你喜欢

转载自blog.csdn.net/weixin_62264287/article/details/125824130