问题描述
java编写 计算球体体积,圆柱体体积,立方体体积 用方法重载编程,我不用重载的会,这重载的就不会了 还是就是形参要用几个
解决方案
public interface Graph{public double volume();}//圆柱public class Cylinder implements Graph{private double radius;public Cylinder(double radius) {this.radius = radius;}@Overridepublic double volume(){//计算体积公式return 0;}}//长方public class Cuboid implements Graph{private double width;private double height;private double length;public Cuboid(double width, double height, double length){this.width = width;this.height = height;this.length = length;}@Overridepublic double volume(){// 公式return 0;}}//工具public class ComputeUtil{public static double computeVolume(Graph graph) {return graph.volume();}}
解决方案二:
关于重载,可以参照下面的代码:public class Volumn{//球体体积public float computeVolumn(float radius){}//圆柱体体积public float computeVolumn(float radius, float height){}//长方体体积(包括立方体)public float computeVolumn(float length, float width, float height){}}