展开全部
Java程序:public class Main {
public static void main(String[] args){
Shape p = null;
p = new Point(4, 4);
((Point)p).displayPosition();
p = new Circle(2, new Point());
System.out.println("圆的面积:" + p.area());
p = new Cylinder(2, 5, new Point());
System.out.println("圆柱e69da5e887aa3231313335323631343130323136353331333363373136体的面积:" + p.area());
System.out.println("圆柱体的体积:" + p.volume());
}
}
/**
* 形状类:抽象类
* @author developer
* @version 2017.05.18
*/
abstract class Shape {
/**
* 返回几何形状的面积
* @return 几何形状的面积
*/
abstract double area();
/**
* 返回几何形状的体积
* @return 几何形状的体积
*/
abstract double volume();
}
/**
* 点类
* @author developer
* @version 2017.05.18
*/
class Point extends Shape {
protected int x;
protected int y;
public Point() {
this.x = 0;
this.y = 0;
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public void displayPosition() {
System.out.printf("点的当前位置:(%d, %d)\n", this.x, this. y);
}
@Override
double area() {
return 0;
}
@Override
double volume() {
return 0;
}
}
/**
* 圆类
* @author developer
* @version 2017.05.18
*/
class Circle extends Shape {
/**
* 半径
*/
protected double radius;
/**
* 圆心坐标
*/
protected Point center;
public Circle() {
this.radius = 0;
this.center = new Point();
}
public Circle(double radius, Point center) {
this.radius = radius;
this.center = new Point(center.x, center.y);
}
@Override
double area() {
return Math.PI * radius * radius;
}
@Override
double volume() {
return 0;
}
}
/**
* 圆柱体类
* @author developer
* @version 2017.05.18
*/
class Cylinder extends Shape {
/**
* 半径
*/
protected double radius;
/**
* 高
*/
protected double height;
/**
* 圆心坐标
*/
protected Point center;
public Cylinder() {
this.radius = 0;
this.height = 0;
this.center = new Point();
}
public Cylinder(double radius, double height, Point center) {
this.radius = radius;
this.height = height;
this.center = new Point(center.x, center.y);
}
@Override
double area() {
return 2 * Math.PI * radius * radius + 2 * Math.PI * radius * height;
}
@Override
double volume() {
return Math.PI * radius * radius * height;
}
}
运行测试:点的当前位置:(4, 4)
圆的面积:12.566370614359172
圆柱体的面积:87.96459430051421
圆柱体的体积:62.83185307179586