JAVA问题,急

来源:百度知道 编辑:UC知道 时间:2024/04/20 11:31:48
class Box{
double width;
double height;
double depth;
Box(double w,double h,double d){
width=w;
height=h;
depth=d;
}
Box(){
width=-1;
height=-1;
depth=-1;
}
Box(double a){
width=height=depth=a;
}
double volume(){
return width*height*depth;
}
}
class Over{
public static void main(String args[]){
Box mybox1=new Box(1,2,3);
Box mybox2=new Box();
Box mycube=new Box(6);
double vol;
vol=mybox1.volume();
System.out.println(vol);
vol=mybox2.volume();
System.out.println(vol);
vol=mycube.volume();
System.out.println(vol);
}
}
这段程序中double volume(){
return width*height*depth;
}
这什么不可换成double volume(){
volume= width*height*depth;
}
return在这里怎么用,还有return都在什么场合用,怎么用? 谢谢大家!!
怎么返回。我说用我那段程序为什么不行,

当函数有返回值时 要返回已声明的函数类型的值
如double volume() 就是个double类型的函数 必须返回 用return
如果要写成volume= width*height*depth; 函数名应该是void volume()

return“返回”