Java语言编写代码

Java语言编写代码

为了100分

//抽象类

public abstract class Container {

protected double r;

public double getR() {

return r;

}

public void setR(double r) {

this.r = r;

}

abstract double volume();

}

//立方体

public class Cube extends Container{

double volume() {

return Math.pow(r, 3);

}

}

//球体

public class Sphere extends Container{

double volume() {

return Math.PI * Math.pow(r, 3) * 4 / 3;

}

}

//Tiji

public class Tiji {

public static void main(String[] args) {

Cube cube = new Cube();

cube.setR(4);

System.out.println("立方体体积" + cube.volume());

Sphere sphere = new Sphere();

sphere.setR(3);

System.out.println("球体体积:" + sphere.volume());

}

}