类的构造函数到底是在什么时间被调用的?? 当然是创建时,确切的讲就是在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 【jar包双击无反应,大家过来看看怎么回事吧。。。谢谢】 列举1 2 3组成数字的所有组合 bangmang 面试给一个问题问倒了 现在作swing有发展前景吗? 求助:关于Image类的问题 求大神用java实现CRC-16校验,输入 AB 12 BC 3D 输出AB 12 BC 3D 31 14这种格式。 求救乱码问题? 如何把tree的左边的小加号和线去掉 润乾报表 3读不了数据库 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