类的构造函数到底是在什么时间被调用的?? 当然是创建时,确切的讲就是在new时,两个vol应该一样的。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果是在创建时,就调用构造函数。那么上述代码的执行结果应该是:Constructing Box...Constructing Box...Volume is 60.0Volume is 60.0可我的执行结果却是:Constructing Box....Volume is 60.0Constructing Box....Volume is 60.0到底时怎么回事???????????? 晕。你看看你的程序: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.0Volume is 60.0 问个正则表达式的菜鸟问题 怎样知道数组的维度?急 jdk1.1 的开发环境或者是用什么工具呀!! 求教:如何提高计算精确度? 各位前辈,关于需求分析的问题,请解答! 一个简单的java程序! 使用MD5加密后的两个相同字符串,为什么比较却不等? 一个很简单的问题! 大家推荐一本servlet的入门电子书籍和资料好不好? 请问如何定义Jtable的单元格宽度? Connection和Statement的关系,求解 help
Constructing Box...
Constructing Box...
Volume is 60.0
Volume is 60.0
可我的执行结果却是:
Constructing Box....
Volume is 60.0
Constructing Box....
Volume is 60.0
到底时怎么回事????????????
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