课堂练习6:
– 定义一个圆类(Circle),其所在的包为bzu.info.software;定义一个圆柱类Cylinder,其所在的包为bzu.info.com;定义一个主类A,其所在的包也为bzu.info.com,在A中生成一个Cylinder对象,并输出其体积。编译并运行该类。
– 试着改变求体积方法的访问权限,查看并分析编译和运行结果
– 把Cylinder类和A类置于不同的包中,通过对求体积方法设置不同的访问权限,查看并分析编译和运行结果
package bzu.info.com;
import bzu.info.software.Circle;
public class A {
public static void main(String[] args) {
Cylinder cylinder=new Cylinder();
Circle circle=new Circle(10);
circle.setR(10);
cylinder.setCircle(circle);
cylinder.setHeight(20);
double s=cylinder.getVolme();
System.out.println("底面半径:"+circle.getR()+",圆柱的高:"
+cylinder.getHeight()+",圆柱的体积:"+s);
}
}
package bzu.info.com;
import bzu.info.software.Circle;
public class Cylinder {
Circle circle;
double height;
public Circle getCircle() {
return circle;
}
public void setCircle(Circle circle) {
this.circle = circle;
}
double getVolme() {
return circle.getArea()*height;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
package bzu.info.software;
public class Circle {
double r,area;
public Circle(double r){
this.r=r;
}
public void setR(double r) {
this.r=r;
}
public double getR() {
return r;
}
public double getArea() {
area=3.14*r*r;
return area;
}
}