猿问

利用方法重载编程

java编写

 

计算球体体积,圆柱体体积,立方体体积

 

用方法重载编程,我不用重载的会,这重载的就不会了

 

还是就是形参要用几个


慕的地6264312
浏览 677回答 2
2回答

MMTTMM

public interface Graph{public double volume();}//圆柱public class Cylinder implements Graph{private double radius;public Cylinder(double radius) {     this.radius = radius; } @Override public 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; } @Override public double volume() {     // 公式     return 0; }}//工具public class ComputeUtil{public static double computeVolume(Graph graph) {return graph.volume();}}

慕田峪9158850

关于重载,可以参照下面的代码:[code="java"]public class Volumn{//球体体积public float computeVolumn(float radius){}//圆柱体体积public float computeVolumn(float radius, float height){}//长方体体积(包括立方体)public float computeVolumn(float length, float width, float height){}
随时随地看视频慕课网APP

相关分类

Java
我要回答