迷茫2017-04-18 09:27:39
If you are pursuing readability, write like this:
pubilc Object get() {
if () {
return A;
}
if () {
return B;
}
return C;
}
伊谢尔伦2017-04-18 09:27:39
Except for poor readability, there is no problem. A method may return different resultsaccording to different situations, but each call will only return one of the results.
A better way to write is to prioritize exception branches in the method body and return the exception result as early as possible.
pubilc Object get(){
//第一个if对应题目中的最后一个else
if(invalidResult1) {
return null;
}
//第二个if对应题目中倒数第二个else
if(invalidResult2) {
return null;
}
//对应题目中第二个if
return succesResult;
}
阿神2017-04-18 09:27:39
One key point is that a method will only return once when called. If your method may return twice, it will not pass compilation. Because the method returns a value, it means that the method has reached the end point and the program will exit the method.
大家讲道理2017-04-18 09:27:39
I think it should be like this
pubilc Object get(){
Object obj=null;
if(){
if(){
obj=x;
}else{
obj=xx;
}
}else{
obj=xxx;
}
return obj;
}