请编写一个程序,定义一个Circle类,成员属性:radius、h、PI,成员方法 volume()能够计算圆柱体的体积。要求圆周率PI定义为最终的变量,值为3.14,赋值的3个方式都写一下。
2条回答 默认 最新
关注 有测试类哈
class Circle { private double radius; public Circle() { } public Circle(double radius) { this.radius = radius; } //求圆的面积 public double getArea() { return 3.14 * radius * radius; } //求圆的周长 public double getPerimeter() { return 2 * 3.14 * radius; } public void show() { System.out.printf("半径为:" + radius + ",周长为:" + getPerimeter() + ",面积为:" + getArea()); } } public class Cylinder extends Circle { private double hight; public Cylinder(double radius, double hight) { super(radius); this.hight = hight; } //获取圆柱体的体积 public double getVolume() { return getArea() * hight; } //将圆柱体的体积输出到屏幕 public void showVolume() { System.out.println("圆柱体的体积为:" + getVolume()); } } class Demo{ public static void main(String[] args) { //创建一个圆柱体对象 Cylinder cylinder = new Cylinder(10.0, 10.0); cylinder.showVolume(); cylinder.show(); } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 微信会员卡等级和折扣规则
- ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
- ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
- ¥15 gdf格式的脑电数据如何处理matlab
- ¥20 重新写的代码替换了之后运行hbuliderx就这样了
- ¥100 监控抖音用户作品更新可以微信公众号提醒
- ¥15 UE5 如何可以不渲染HDRIBackdrop背景
- ¥70 2048小游戏毕设项目
- ¥20 mysql架构,按照姓名分表
- ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分