关于return的用法
程序代码:
public class Box {
int length,width, height,volume;
int v;
public Box(int length, int width, int height) {
super();
this.length = length;
this.width = width;
this.height = height;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getvolume() {
return volume;
}
public void setvolume(int volume) {
this.volume = volume;
}
public int calcVolume(){
v=length*width*height;
System.out.println(v);
return v;
}
public void print(){
System.out.println(v);
}
public static void main(String[] args) {
Box box=new Box(10,15,12);
box.print();
box.calcVolume();
}
}
运行结果是:int length,width, height,volume;
int v;
public Box(int length, int width, int height) {
super();
this.length = length;
this.width = width;
this.height = height;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getvolume() {
return volume;
}
public void setvolume(int volume) {
this.volume = volume;
}
public int calcVolume(){
v=length*width*height;
System.out.println(v);
return v;
}
public void print(){
System.out.println(v);
}
public static void main(String[] args) {
Box box=new Box(10,15,12);
box.print();
box.calcVolume();
}
}
0
1800
return 在这里要怎么用
为什么是0?
坐等高手回答~
----------------解决方案--------------------------------------------------------
你调用box.print()的时候v还没有算出来呢
当然是0
----------------解决方案--------------------------------------------------------
那么怎么调用box.print()方法呢?
为什么没有算出来?
程序不是顺序执行呢吗?
----------------解决方案--------------------------------------------------------
public int calcVolume(){
v=length*width*height;
System.out.println(v);
return v;
}
你的calcVolume方法是用来计算v的
结果你在调用print之后才调用的calcVolume
那么你说为什么调用print的时候v没算出来呢?
----------------解决方案--------------------------------------------------------
明白了~~
谢谢了~~
----------------解决方案--------------------------------------------------------
路过学习学习
----------------解决方案--------------------------------------------------------
输出的初值 没有任何处理
----------------解决方案--------------------------------------------------------