package 圓;
public class circle {
private double Radius;
public double getRadius(){
return Radius;
};
public void setRadius(double r) {
Radius= r;
}
public circle(double r){
Radius=r;
}
public double getPerimeter(){//底圓周長
return 2 * Math.PI * Radius;
}
public double getArea(){//面積
return Math.PI *Radius *Radius;
}
public void show( ){
System.out.println("圓的半徑: " +getRadius());
System.out.println("圓的面積: " +getArea());
System.out.println("圓的周長: " +getPerimeter());
}
}繼承
package 圓;
public class yuanzhu extends circle {//圓柱類繼承圓類
private double height;
public double getheight() {
return height;
}
public void setheight(double h) {
height = h;
}
public yuanzhu(double r,double h){
super(r);
height=h;
}
public double getVolume(){//獲得體積
return getArea() * height;
}
public void showVolume( ){
System.out.println("圓柱體的體積:" +getVolume());
}
}
package 圓;
public class MAIN {
public static void main(String[] args) {
circle d1 = new circle(2);//半徑初始化爲3
d1.show( );
yuanzhu d2 =new yuanzhu(3,6);//高初始化爲6
d2.showVolume();
}
}ci