Topic: 这一小段代码有点看不懂,求助高手帮忙分析(return 的作用)

  Print this page

1.这一小段代码有点看不懂,求助高手帮忙分析(return 的作用) Copy to clipboard
Posted by: zmj888
Posted on: 2005-12-08 10:55

public class Triple {

private int a1;
private int a2;
private int a3;

public Triple(){
this.( 0, 0, 0 );
}

public Triple( int a, int b, int c ) {
setValue( 1, a );
setValue( 2, b );
setValue( 3, c );
}

public int getValue( int i ) {
switch( i ) {
case 1: return a1;
case 2: return a2;
case 3: return a3;
default:
System.err.println("error");
System.exit( i );
return i;
}
}
public void setValue( int i , int value ) {
switch( i ) {
case 1: a1 = value;return;
case 2: a2 = value;return;
case 3: a3 = value;return;
default:
System.err.println("error");
System.exit( i );
}

}

我想知道上面红色return 的作用,能否换成break

2.Re:这一小段代码有点看不懂,求助高手帮忙分析(return 的作用) [Re: zmj888] Copy to clipboard
Posted by: Pink
Posted on: 2005-12-09 00:06

既然返回类型是void,应该就不需要return了吧

3.Re:这一小段代码有点看不懂,求助高手帮忙分析(return 的作用) [Re: zmj888] Copy to clipboard
Posted by: ftang
Posted on: 2005-12-09 07:18

ho~~~~~com'on...Comrade PINK, return here is very important...without return the logic is complete wrong, so we all know the when using case, if no "break" after each the case condition the logic will fall down, "return" here is stop the code run farther, but different is if he use break instead of return then the code will will run System.exit( i ); and this is WRONG....
General Speaking....answer the question carefully...

4.Re:这一小段代码有点看不懂,求助高手帮忙分析(return 的作用) [Re: zmj888] Copy to clipboard
Posted by: zxasqw
Posted on: 2005-12-09 22:38

可以,return 有两种用法
1。在方法中返回一个值
2。在if 中返回
你这里属于后者
这个时候和break作用同

5.Re:这一小段代码有点看不懂,求助高手帮忙分析(return 的作用) [Re: zmj888] Copy to clipboard
Posted by: ftang
Posted on: 2005-12-09 23:26

NO!!!...here you can't use break...it is different here...if you use break, then System.exitLight Bulb will be executed...which obviously not correct logic...

PLEASE MAKE SURE WHAT ARE YOU TALKING ABOUT BEFORE U POST

6.Re:这一小段代码有点看不懂,求助高手帮忙分析(return 的作用) [Re: ftang] Copy to clipboard
Posted by: why
Posted on: 2005-12-10 00:31

ftang wrote:
NO!!!...here you can't use break...it is different here...if you use break, then System.exit(i) will be executed...which obviously not correct logic...

PLEASE MAKE SURE WHAT ARE YOU TALKING ABOUT BEFORE U POST

:?) Yes, can use break for return in this case, why not?
System.exit(i); won't be executed unless the break is omitted.


zxasqw wrote:
可以,return 有两种用法
1。在方法中返回一个值
2。在if 中返回
你这里属于后者
这个时候和break作用同

Right and wrong.
It is correct that 这个时候和break作用同
but return is not limited to 在if 中返回... you could use it anyware in your Java program.

return
Definition: A Java language keyword used to end the execution of a method and return program execution to the next method up the call stack. It can return an optional value.
http://java.about.com/od/faqsanddocumentation/g/return.htm

7.Re:这一小段代码有点看不懂,求助高手帮忙分析(return 的作用) [Re: zmj888] Copy to clipboard
Posted by: zw_ren
Posted on: 2005-12-13 08:49

在这个地方,return 和break的作用是一样的,但是建议使用break,因为一个方法返回的地方越少越好。假设你想把它改了,改成返回类型为int , 这时你要修改所有出现return 的地方。

虽然此处return和break起到的作用一样,但是在底层来说,还是有区别的。
break可能会实现为一个跳转,跳转至switch结束的地方。在这段程序中switch结束就到方法最后了,底层会在最后加上返回指令。

return则是直接在出现的地方加上返回指令,所以严格来说,它们的流程不一样的。

8.Re:这一小段代码有点看不懂,求助高手帮忙分析(return 的作用) [Re: zmj888] Copy to clipboard
Posted by: blueoxygen
Posted on: 2005-12-14 09:42

我也疑惑中,为啥ftang 说不可以?


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923