class Circle{
private double radius;
public void Circle() {
this.radius = 0;
}
public Circle(double x) {
radius = x;
}
public double getRadius() {
return radius;
}
public double getPerimeter() {
return radius*2*Math.PI;
}
public double getArea() {
return radius*radius*Math.PI;
}
public void disp() {
System.out.println("圆的半径为:"+radius+"\n圆的周长为:"+getPerimeter()+"\n圆的面积为:"+getArea());
}
}
public class yuan {
public static void main(String[] args) {
Circle per = new Circle(2);
per.getRadius();
per.disp();
}
class Cylinder extends Circle {
private double height;
public Cylinder(double r,double h) {
super(r);
height = h;
}
public double getHeight() {
return height;
}
public double getVol() {
return getArea()*height;
}
public void dispVol() {
System.out.println("圆柱的体积为:"+getVol());
}
}
public class jichengyuan {
public static void main(String[] args) {
Cylinder der = new Cylinder(1.0,2.0);
der.dispVol();
}
}
import java.util.Scanner;
class Cylinder2 extends Circle {
private double height;
public Cylinder2(double r,double h) {
super(r);
height = h;
}
public double getHeight() {
return height;
}
public double getVol() {
return getArea()*height;
}
public void dispVol() {
System.out.println("圆柱的体积为:"+getVol());
}
}
public class jichengyuan2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Cylinder der = new Cylinder(sc.nextDouble(),sc.nextDouble());
der.dispVol();
}
}
原文:https://www.cnblogs.com/wuguijunniubi/p/11631404.html