根据下图实现类。在CylinderTest类中创建Cylinder类的对象,设置圆柱的底面半径和高,并输出圆柱的体积。
Circle (圆) -radius :double
Circle(): 构造器,将radius属性初始化为1
+setRadius(double radius) : void
+getRadius(): double
+findArea():double 计算圆的面积
Cylinder (圆柱) -length:double
Cylinder(): 构造器,将length属性初始化为1
+setLength(double length):void
+getLength():double
+findVolume() :double 计算圆柱体积
Circle.java
package com.company;
public class Circle {
/*
根据下图实现类。在CylinderTest类中创建Cylinder类的对象,设置圆
柱的底面半径和高,并输出圆柱的体积。
Circle (圆) -radius :double
Circle(): 构造器,将radius属性初始化为1
+setRadius(double radius) : void
+getRadius(): double
+findArea():double 计算圆的面积
*/
double radius;
public Circle(){ //无参构造
this.radius = 1;// 题意要求
}
public Circle(double radius){// 带参构造
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public void findArea(){ //直接输出
System.out.println("半径为:" + radius + "的圆的面积为:" + Math.PI * radius * radius);
}
public double getArea(){ //返回面积值
return Math.PI * radius * radius;
}
}
Cylinder.java
package com.company;
public class Cylinder extends Circle{
/*
Cylinder (圆柱) -length:double
Cylinder(): 构造器,将length属性初始化为1
+setLength(double length):void
+getLength():double
+findVolume() :double 计算圆柱体积
*/
private double length;
public Cylinder(){
this.length = 1;// 题意要求
}
public Cylinder(double length){
this.length = length;
}
public Cylinder(double radius,double length){ //品一下
this.length = length;
this.radius = radius;
}
public double getLength() {
return length;
}
@Override
public void findArea() { //方法的重写
System.out.println("半径为:" + radius + "的圆柱的底面积为:" + Math.PI * radius * radius);
}
public void findVolume(){ //直接打印体积
System.out.println("该圆柱的体积为:" + getArea() * length);
}
}
Main.java
package com.company;
public class Main {
/**
* @param args
* @lzx
*/
public static void main(String[] args) {
Cylinder cylinder = new Cylinder(3,6);
cylinder.findArea(); //圆柱底面积
cylinder.findVolume(); //圆柱体积
}
}