慕的地6264312
2019-03-21 18:15:27
java編寫 計算球體體積,圓柱體體積,立方體體積 用方法重載編程,我不用重載的會,這重載的就不會了 還是就是形參要用幾個
2 回答

MMTTMM
TA貢獻1869條經驗 獲得超4個贊
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
TA貢獻1794條經驗 獲得超8個贊
關于重載,可以參照下面的代碼:
[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){
}
添加回答
舉報
0/150
提交
取消