当然是创建时,确切的讲就是在new时,两个vol应该一样的。

解决方案 »

  1.   

    如果是在创建时,就调用构造函数。那么上述代码的执行结果应该是:
    Constructing Box...
    Constructing Box...
    Volume is 60.0
    Volume is 60.0
    可我的执行结果却是:
    Constructing Box....
    Volume is 60.0
    Constructing Box....
    Volume is 60.0
    到底时怎么回事????????????
      

  2.   

    晕。你看看你的程序:
    class Box {                                                     
    double width,height,depth;                              
    Box() {                                                 
    width=10;                                       
    height=3;                                       
    depth=2;                                        
    }                                                       
    double volume(){                                        
    System.out.println("Constructing Box....");     
    return width*height*depth;                      
    }                                                       
    }你的那句System.out.println("Constructing Box....");是在volume这个method中写的,当然是在调用这个method时执行System.out.println("Constructing Box....");了。而你的构造函数根本没有println语句。我估计你是写错地方了。你想要打出Constructing Box....这句话,而实际上,根据这句话的意思应该把System.out.println("Constructing Box....");放在构造函数中去。              
    像这样:class Box {                                                     
    double width,height,depth;                              
    Box() {                 
    System.out.println("Constructing Box....");                                     
    width=10;                                       
    height=3;                                       
    depth=2;                                        
    }                                                       
    double volume(){
    return width*height*depth;                      
    }                                                       
    }  
    结果便是:
    Constructing Box...
    Constructing Box...
    Volume is 60.0
    Volume is 60.0