java吧 关注:1,272,341贴子:12,783,237
  • 14回复贴,共1

新手,自己看到方法重载,自己看题写了个,不知掉哪错了求指点。

只看楼主收藏回复

//方法重载
class Cylinder{
private double radius;
private int height;
private double pi;
private String color;
public double SetCylinder(double r,int h){
radius=r;
height=h;
return r+h;
}
public void SetCylinder(String c){
color=c;
}
double area(){
return pi*radius*radius;
}
double volume(){
return area()*height;
}
public void show(){
System.out.println("圆柱颜色="+color);
}
}
public class text3 {
public static void main(String[] args){
Cylinder volu = new Cylinder();
volu.SetCylinder("红色");
System.out.println("高和半径之和="+volu.SetCylinder(2.5,5));
System.out.println("圆柱体面积="+volu.area());
System.out.println("圆柱体体积="+volu.volume());
volu.show();
}
}



IP属地:北京1楼2012-05-29 18:46回复
    private 的属性 必须通过get方法取得


    IP属地:重庆2楼2012-05-29 19:04
    回复
      2025-08-03 15:20:07
      广告
      不感兴趣
      开通SVIP免广告
      不信 你把private 去掉


      IP属地:重庆3楼2012-05-29 19:04
      回复
        我信 我就不去掉!


        IP属地:辽宁4楼2012-05-29 19:05
        回复
          去掉了还是不对啊


          IP属地:北京5楼2012-05-29 19:28
          回复

            去掉了还是不对啊


            IP属地:北京6楼2012-05-29 19:29
            回复
              The type Cylinder is already defined


              IP属地:北京7楼2012-05-29 19:29
              回复
                嗯 运行了一下你的代码 没有错误啊


                IP属地:重庆8楼2012-05-29 19:44
                回复
                  2025-08-03 15:14:07
                  广告
                  不感兴趣
                  开通SVIP免广告
                  擦电脑有问题了~踹了他。


                  IP属地:北京9楼2012-05-29 19:48
                  回复
                    哦 原来是你的 double 变量没有赋值


                    IP属地:重庆10楼2012-05-29 19:50
                    回复
                      给你修改了一下
                      //方法重载
                      class Cylinder{
                      private double radius;
                      private int height;
                      private double pi = 2;
                      private String color;
                      public double SetCylinder(double radius,int height){
                      this.radius =radius;
                      this.height=height;
                      return height+radius;
                      }
                      public void SetCylinder(String color){
                      this.color=color; }
                      double area(){
                      return pi*radius*radius;
                      }
                      double volume(){
                      return area()*height;
                      }
                      public void show(){
                      System.out.println("圆柱颜色="+color);
                      }
                      }
                      public class Demo01 {
                      public static void main(String[] args){
                      Cylinder volu = new Cylinder();
                      volu.SetCylinder("红色");
                      System.out.println("高和半径之和="+volu.SetCylinder(3.13,3));
                      System.out.println("圆柱体面积="+volu.area());
                      System.out.println("圆柱体体积="+volu.volume());
                      volu.show();
                      }
                      }
                      刚学基础时候 最好使用 editplus + dos
                      double 默认 是0的
                      所以pi为0 0乘以任何数都为0
                      所以你的area 的 返回值为0
                      于是下面的2个体积 都是0.


                      IP属地:重庆11楼2012-05-29 19:52
                      回复
                        过来砸我场子吗


                        IP属地:重庆12楼2012-05-29 19:53
                        回复


                          13楼2012-05-29 20:21
                          回复
                            这题视乎早出现过 不过忘了....


                            IP属地:重庆14楼2012-05-29 22:59
                            回复
                              2025-08-03 15:08:07
                              广告
                              不感兴趣
                              开通SVIP免广告
                              哈哈我也找到了


                              IP属地:北京15楼2012-05-30 15:25
                              回复