根据下图实现类。在CylinderTest类中创建Cylinder类的对象,设置圆柱的底面半径和高,并输出圆柱的体积,继承性

根据下图实现类。在CylinderTest类中创建Cylinder类的对象,设置圆柱的底面半径和高,并输出圆柱的体积,继承性

Circle类
public class Circle {
	private double radius;//圆的半径
	
	public Circle(){//radius属性初始化为1
		radius = 1.0;
	}
	
	public double getRadius() {
		return radius;
	}
	public void setRadius(double radius) {
		this.radius = radius;
	}
	public double findArea(){//求圆的面积
		 double area = Math.PI * radius * radius;
		 return area;
	}
}

Circle类
public class Cylinder extends Circle{
	private double length;

	public Cylinder() {//length属性初始化为1
		length = 1.0;
	}

	public double getLength() {
		return length;
	}

	public void setLength(double length) {
		this.length = length;
	}
	public double findVolume(){//求圆柱的体积
		//return Math.PI * getRadius() * getRadius() * length;
		 return findArea() * length;
	}
	
}
CylinderTest测试类
public class CylinderTest {
	public static void main(String[] args) {
		Cylinder cylinder = new Cylinder();
		cylinder.setRadius(10);
		cylinder.setLength(10);
		System.out.println("圆柱的体积: " + cylinder.findVolume());
		System.out.println("底面圆的面积:" + cylinder.findArea());
	}
}

上一篇:Java static关键字的小练习


下一篇:c++圆形