Java教程

Java语言程序设计——面向抽象编程:

本文主要是介绍Java语言程序设计——面向抽象编程:,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1、设计一个抽象类Geometry,该类包括抽象方法getArea(),用于计算面积,getCircum()方法,用于计算周长;

2、设计一个柱状类Pillar,在类中使用抽象类声明变量,该类具有求体积方法getVolume(),用于求柱体积;具有求面积方法getAreas(),用于求柱体表面积。

3、设计类:圆,矩形,正方形,三角形;这些类继承抽象类Geometry,重写getArea()和getCircum()方法。

4、设计一个类AppTest,使用上转型对象来计算圆柱体积和表面积,圆柱底      面半径至少10组数据;计算矩形柱的体积和表面积,矩形边长自己确定数据,数据不少于10组;同样,计算正方形柱体和三角形柱体,数据不少于10组。

Geometry类

package com.java11.abst;
public abstract class Geometry {
    public Double getArea(){
        return null;//计算面积
    }
    public Double getCircum(){
        return null;//计算周长
    }
}

Pillar类 

package com.java11.abst;
public abstract class Pillar {
    public abstract Double getVolume();//计算面积
    public abstract Double getAreas();//求柱体表面积
}

圆形实现

package com.java11.geometry;
import com.java11.abst.Geometry;
/**
 * 圆形实现
 */
public class Circle extends Geometry {
    private Double r;
    private Double PI = Math.PI;
    public Double getR() {
        return r;//半径
    }
    /**
     * 构造函数
     * @param r
     */
    public Circle(Double r){
        this.r = r;
    }
    /**
     * 求面积
     * @return
     */
    @Override
    public Double getArea() {
        return PI * r * r;
    }
    /**
     * 求周长
     * @return
     */
    @Override
    public Double getCircum() {
        return 2 * PI * r;
    }
}

矩形实现

package com.java11.geometry;
import com.java11.abst.Geometry;
/**
 * 矩形实现
 */
public class Rectangle extends Geometry {
    private Double length; //长
    private Double width;//宽
    public Double getLength() {
        return length;
    }
    public Double getWidth() {
        return width;
    }
    /**
     * 构造函数
     * @param length
     * @param width
     */
    public Rectangle(Double length, Double width){
        this.length = length;
        this.width = width;
    }
    /**
     * 求面积
     * @return
     */
    @Override
    public Double getArea() {
        return length * width;
    }
    /**
     * 求周长
     * @return
     */
    @Override
    public Double getCircum() {
        return (length * 2) + (width * 2);
    }
}

正方形实现

package com.java11.geometry;
import com.java11.abst.Geometry;
/**
 * 正方形实现
 */
public class Square extends Geometry {
    private Double sideLength;//边长
    public Double getSideLength() {
        return sideLength;
    }
    /**
     * 构造函数
     * @param sideLength
     */
    public Square(Double sideLength){
        this.sideLength = sideLength;
    }
    /**
     * 求面积
     * @return
     */
    @Override
    public Double getArea() {
        return sideLength * sideLength;
    }
    /**
     * 求周长
     * @return
     */
    @Override
    public Double getCircum() {
        return sideLength * 4;
    }
}

三角形实现

package com.java11.geometry;
import com.java11.abst.Geometry;
/**
 * 三角形实现
 */
public class Triangle extends Geometry {
    private Double a;//a边
    private Double b;//b边
    private Double bottom;//c边(底)
    private Double height;//高
    public Double getA() {
        return a;
    }
    public Double getB() {
        return b;
    }
    public Double getBottom() {
        return bottom;
    }
    public Double getHeight() {
        return height;
    }
    /**
     * 构造函数
     * @param a
     * @param b
     * @param bottom
     * @param height
     */
    public Triangle(Double a, Double b, Double bottom, Double height){
        this.a = a;
        this.b = b;
        this.bottom = bottom;
        this.height = height;
    }
    /**
     * 求面积
     * @return
     */
    @Override
    public Double getArea() {
        return (bottom * height) / 2;
    }
    /**
     * 求周长
     * @return
     */
    @Override
    public Double getCircum() {
        return a + b + bottom;
    }
}

 正方形柱体积实现

package com.java11.pillar;
import com.java11.abst.Pillar;
import com.java11.geometry.Square;
/**
 * 正方形柱体积实现
 */
public class Cube<E> extends Pillar {
    E bottom;//底面积
    public Cube(E bottom){
        this.bottom = bottom;
    }
    @Override
    public Double getVolume() {
        return ((Square) bottom).getSideLength() * ((Square) bottom).getSideLength() * ((Square) bottom).getSideLength();
    }
    @Override
    public Double getAreas() {
        return 6 * ((Square) bottom).getSideLength() * ((Square) bottom).getSideLength();
    }
}

矩形柱体积实现

package com.java11.pillar;
import com.java11.abst.Pillar;
import com.java11.geometry.Rectangle;
/**
 * 矩形柱体积实现
 */
public class Cuboid<E> extends Pillar {
    Double height;//矩形高度
    E bottom;//底面积
    public Cuboid(E bottom, Double height){
        this.bottom = bottom;
        this.height = height;
    }
    @Override
    public Double getVolume() {
        return ((Rectangle) bottom).getArea() * height;
    }
    @Override
    public Double getAreas() {
        Double length = ((Rectangle) bottom).getLength();
        Double width = ((Rectangle) bottom).getWidth();
        return (length * width * 2) + (width * height * 2) + (length * height * 2);
    }
}

 圆柱体积实现

package com.java11.pillar;
import com.java11.abst.Pillar;
import com.java11.geometry.Circle;
/**
 * 圆柱体积实现
 */
public class Cylinder<E> extends Pillar {
    Double height;//圆柱体高度
    E bottom;//底面积
    public Cylinder(E bottom, Double height){
        this.bottom = bottom;
        this.height = height;
    }
    @Override
    public Double getVolume() {
        return ((Circle) bottom).getArea() * height / 3;
    }
    @Override
    public Double getAreas() {
        return (((Circle) bottom).getArea() * 2) + (((Circle) bottom).getCircum() * height);
    }
}

三角形柱体积实现

package com.java11.pillar;
import com.java11.abst.Pillar;
import com.java11.geometry.Triangle;
/**
 * 三角形柱体积实现
 */
public class TriangularPrism<E> extends Pillar {
    Double height;//三角形高度
    E bottom;//底面积
    public TriangularPrism(E bottom, Double height){
        this.bottom = bottom;
        this.height = height;
    }
    @Override
    public Double getVolume() {
        return ((Triangle) bottom).getArea() * height;
    }
    @Override
    public Double getAreas() {
        Double a = ((Triangle) bottom).getA();
        Double b = ((Triangle) bottom).getB();
        Double c = ((Triangle) bottom).getBottom();
        return (a * height) + (b * height) + (c * height) + (2 * ((Triangle) bottom).getArea());
    }
}

AppTest类

package com.java11;
import com.java11.geometry.Circle;
import com.java11.geometry.Rectangle;
import com.java11.geometry.Square;
import com.java11.geometry.Triangle;
import com.java11.pillar.Cube;
import com.java11.pillar.Cuboid;
import com.java11.pillar.Cylinder;
import com.java11.pillar.TriangularPrism;
import java.text.DecimalFormat;
public class AppTest {
    public static void main(String[] args) {
        calcCylinder();//圆柱体
        System.out.println("\n********************************************\n");
        calcCuboid();//矩形
        System.out.println("\n********************************************\n");
        calcCube();//正方体
        System.out.println("\n********************************************\n");
        calcTriangularPrism();//三角形
    }
    /**
     * 计算圆柱体测试结果
     */
    public static void calcCylinder(){
        //测试数据:半径
        Double[] rs = new Double[]{5.0,13.0,14.0,23.0,12.32,19.57,33.12,29.85,13.73,17.0};
        //测试数据:柱体高度
        Double[] heights = new Double[]{15.0,27.22,13.53,21.88,9.32,18.61,14.21,24.74,16.81,21.99};
        for(int i = 0; i < rs.length; i++){
            if(i >= heights.length){
                break;
            }
            Circle circle = new Circle(rs[i]);
            Cylinder<Circle> cylinder = new Cylinder<Circle>(circle,heights[i]);
            DecimalFormat df = new DecimalFormat("#.00");
            System.out.println("圆柱体第"+(i+1)+"次测试,半径:"+rs[i]+",柱体高度:"+ heights[i] +";体积:" + df.format(cylinder.getVolume()) + ";表面积:" + df.format(cylinder.getAreas()));
        }
    }
    /**
     * 计算矩形测试结果
     */
    public static void calcCuboid(){
        //测试数据:长
        Double[] lengths = new Double[]{5.0,31.25,13.0,21.92,23.18,25.21,17.02,15.83,29.33,31.75};
        //测试数据:宽
        Double[] widths = new Double[]{13.21,21.0,12.5,18.0,27.22,36.1,10.8,15.0,17.21,16.5};
        //测试数据:柱体高度
        Double[] heights = new Double[]{17.0,21.22,12.53,19.88,11.32,27.61,21.21,10.74,8.81,17.99};
        for(int i = 0; i < lengths.length; i++){
            if(i >= widths.length || i >= heights.length){
                break;
            }
            Rectangle rectangle = new Rectangle(lengths[i],widths[i]);
            Cuboid<Rectangle> cuboid = new Cuboid<Rectangle>(rectangle,heights[i]);
            DecimalFormat df = new DecimalFormat("#.00");
            System.out.println("矩形第"+(i+1)+"次测试,长:"+lengths[i]+",宽:"+widths[i]+",柱体高度:"+ heights[i] +";体积:" + df.format(cuboid.getVolume()) + ";表面积:" + df.format(cuboid.getAreas()));
        }
    }
    /**
     * 计算正方体测试结果
     */
    public static void calcCube(){
        //测试数据:边长
        Double[] sideLengths = new Double[]{13.21,21.0,12.5,18.0,27.22,36.1,10.8,15.0,17.21,16.5};
        for(int i = 0; i < sideLengths.length; i++){
            Square square = new Square(sideLengths[i]);
            Cube<Square> cube = new Cube<Square>(square);
            DecimalFormat df = new DecimalFormat("#.00");
            System.out.println("正方体第"+(i+1)+"次测试,边长:"+sideLengths[i]+";体积:" + df.format(cube.getVolume()) + ";表面积:" + df.format(cube.getAreas()));
        }
    }
    /**
     * 计算三角形测试结果
     */
    public static void calcTriangularPrism(){
        //测试数据:a边
        Double[] as = new Double[]{5.0,31.25,13.0,21.92,23.18,25.21,17.02,15.83,29.33,31.75};
        //测试数据:b边
        Double[] bs = new Double[]{13.21,21.0,12.5,18.0,27.22,36.1,10.8,15.0,17.21,16.5};
        //测试数据:c边(底边)
        Double[] bottoms = new Double[]{16.72,42.33,12.5,33.22,27.22,31.86,10.8,15.0,17.21,16.5};
        //测试数据:三角形高度
        Double[] triangleHeights = new Double[]{17.0,21.22,12.53,19.88,11.32,27.61,21.21,10.74,8.81,17.99};
        //测试数据:柱体高度
        Double[] heights = new Double[]{17.0,21.22,12.53,19.88,11.32,27.61,21.21,10.74,8.81,17.99};
        for(int i = 0; i < as.length; i++){
            if(i >= bs.length || i >= bottoms.length || i >= triangleHeights.length || i >= heights.length){
                break;
            }
            Triangle triangle = new Triangle(as[i],bs[i],bottoms[i],triangleHeights[i]);
            TriangularPrism<Triangle> triangularPrism = new TriangularPrism<Triangle>(triangle,heights[i]);
            DecimalFormat df = new DecimalFormat("#.00");
            System.out.println("三角形第"+(i+1)+"次测试,a边:"+as[i]+",b边:"+bs[i]+",c边(底边):"+bottoms[i]+",底部高度:"+triangleHeights[i]+",柱体高度:"+ heights[i] +";体积:" + df.format(triangularPrism.getVolume()) + ";表面积:" + df.format(triangularPrism.getAreas()));
        }
    }
}

运行结果

 说明面向抽象编程的优点和开闭原则在本设计中的使用情况?

答:(1)面向抽象编程的优点是具体类可从抽象类自动得到这些方法的缺省实现,使开发过程能够更加清晰和灵活。

(2)开闭原则在本次设计中的使用情况:开闭原则规定类或者方法,函数等应该对于扩展是开放的,但是对于修改是封闭的。在本次设计中,主要用于几何体的抽象实现,例如实现圆柱体的处理类时,必须要实现继承的几何体抽象类中的所有方法,但是可以在圆柱体的实现类中加自己的扩展方法,例如构造函数等。


如以上有错误的地方,请在评论区中指出,谢谢!

小可爱们看完点个赞再走一走~~

这篇关于Java语言程序设计——面向抽象编程:的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!