请大家为我解析一下, 说的详细我不怪你!!!
package bbx;public class Box {
    private int chang;
    private int kuan;
    private int gao;
public int getChang() {
return chang;
}
public void setChang(int chang) {
this.chang = chang;
}
public int getGao() {
return gao;
}
public void setGao(int gao) {
this.gao = gao;
}
public int getKuan() {
return kuan;
}
public void setKuan(int kuan) {
this.kuan = kuan;
}
    public int computeArea(){  //表面积
     return chang*kuan*2+chang*gao*2+kuan*gao*2;
    }
    public int computeVolume(){  //体积
     return chang*kuan*gao;
    }
    public void display(){
     System.out.println("长"+chang);
     System.out.println("宽"+kuan);
     System.out.println("高"+gao);
        
    }
}测试类:
package bbx;public class Test { /**
 * @param args
 */
public static void main(String[] args) {
Box bx;
bx=new Box();
bx.setChang(5);
bx.setGao(6);
bx.setKuan(4);
bx.display();
bx.computeArea();
bx.computeVolume();
}}
打印结果是:
长5
宽4
高6问:1.为什么我不可以调用返回的那个方法,打印不出来表面积和体积?
    2.应该怎么写?为什么?(我是初学者,找不到人问!!没办法!!谢谢大家帮帮小弟!)

解决方案 »

  1.   

    因为你的表面积和体积都是在计算啊,并没有打印呢。另外,不建议在所调用的方法中使用 System.out.printlnpublic class Test {    public static void main(String[] args) {
            Box box = new Box(3, 4, 5);
            System.out.println("盒子:" + box.toString());
            System.out.println("表面积:" + box.getArea());
            System.out.println("体积:" + box.getVolume());
        }
    }class Box {
        private int length;
        private int width;
        private int height;
        
        private int area = -1;
        private int volume = -1;
        
        public Box(int length, int width, int height) {
            this.length = length;
            this.width = width;
            this.height = height;
        }
        
        public int getArea() {
            if(area == -1) {
                area = 2 * (length * width + length * height + width * height);
            }
            return area;
        }
        
        public int getVolume() {
            if(volume == -1) {
                volume = length * width * height;
            }
            return volume;
        }
        
        public String toString() {
            return "长:" + length + ",宽:" + width + ",高:" + height;
        }
        
        public int getLength() {
            return length;
        }
        public int getWidth() {
            return width;
        }
        public int getHeight() {
            return height;
        }
    }
      

  2.   

    像 Box 这样的类,按照实际的情况来看,长、宽、高都是固定不变的,也没有必要提供 set 方法了,
    可以在构造方法中就全部设定。
      

  3.   

    你都没有写System.out.println,没有打印语句怎么打印表面积跟体积?
      

  4.   

    thanks。
    我在测试类加上这个就可以了
    System.out.println(bx.computeArea());
    System.out.println(bx.computeVolume());
      谢谢啊。
      真是一言惊醒梦中人啊
      

  5.   

    兄弟,你是return 啦,但你没接收返回值啊。建议:接收后打印,或者是在打印语句里调用那两个方法。只是建议
      

  6.   

    我也写了一个 
    先创建一个抽象类, 然后用Box类继承这个抽象类, 再创建Test类abstract class A
    {
    public abstract void getArea();
    public abstract void getVolume();
    }class Box extends A
    {
    private int length;
    private int width;
    private int height;

    public Box(int length,int width,int height)
    {
    this.length=length;
    this.width=width;
    this.height=height;
    }

    public int getLength()
    {
    return length;
    }
    public int getWidth()
    {
    return width;
    }
    public int getHeight()
    {
    return height;
    }

    public void getArea()
    {
    int area=2 * (length * width + length * height + width * height);
    System.out.println ("Area:"+area);
    }
    public void getVolume()
    {
    int volume=length*width*height;
    System.out.println ("Volume:"+volume);
    }
    }//创建对象
    public class Test{
    public static void main (String[] args) {
    Box box=new Box(3,4,5);
    System.out.println ("Box:"+" Length:"+box.getLength()+" Width:"+box.getWidth()+" Height:"+box.getHeight());
    box.getArea();
    box.getVolume();
        }
    }
      

  7.   

    结果是Box: Length:3 Width:4 Height:5
    Area:94
    Volume:60
      

  8.   

    你的
    bx.computeArea(); 
    bx.computeVolume(); 
    返回整型值但是没有输出