应该这样写:
class Box{
double width ;
double heigth ;
double depth ;
void volume(){
System.out.print("volume is ") ;
System.out.println(width * heigth * depth) ;


}
void setDim(double wid,double hei,double depth){
width = wid; 
heigth = hei ;
depth = depth ;
}
}class BoxDemo{
public static void main(String[] lianga){
Box mybox = new Box() ;
mybox.width = 10;
mybox.heigth = 20;
mybox.depth = 30;
// mybox.setDim(10,20,30) ;
mybox.volume();


}
}

解决方案 »

  1.   

    可能是你Box里的三个变量和某个隐含的东西名字象同吧,你把他们最后的h都去掉试一下,或者在setDim改为this.width = ....,把前面加上this试一下。
    一个很简单的问题,却很难搞明白到底是为什么。
      

  2.   

    class Box{
    double widt;
    double heigt;
    double dept;
    public Box(){}
    void volume(){
    System.out.print("volume is ");
    double volume = width * heigth * depth;
    System.out.println(volume) ;


    }
    void setDim(double wid,double hei,double depth){
    widt = wid; 
    heigt = hei ;
    dept = depth ;
    }
    }
      

  3.   

    上面的老大们,人家是问为什么,不是问怎样改,有点文不对题了噢^_^我想可能是在main()中的问题,对象mybox,有三个示例变量,但是在引用变量时出现了问题,在setDim()中,可能为这三个变量赋值了,可是在随后的
    volume()中,没有使用这三个赋了值的变量,而是使用了在类Box中的变量,因为这几个没有赋值,所以运算出的结果不是理想中的结果
      

  4.   

    哈哈!错在setDim(double wid,double hei,double depth)方法,你将最后的depth参数改为其他的好dth好了。在你原先的这个方法里,java按优先法则取了类变量赋值了。depth还是0阿!
      

  5.   

    void setDim(double wid,double hei,double depth){
    width = wid; 
    heigth = hei ;
    depth = depth ;
    你的上面的double depth的depth换个名字就可以了!因为一般默认情况下初始化数据为0
      

  6.   

    是你的类变量和方法参数相同,楼上两位说的对depth = depth ;实际上并没有对类变量depth赋值,或者把函数参数改一下名,不过按照规范最好同名,改为:
    this.depth = depth;就可以了。