17.5.8 Method body

王朝other·作者佚名  2006-05-09
窄屏简体版  字體: |||超大  

The method-body of a method declaration consists of either a block or a

semicolon.

Abstract and external method declarations do not provide a method

implementation, so their method bodies

simply consist of a semicolon. For any other method, the method body is a

block (§15.2) that contains the

statements to execute when that method is invoked.

When the return type of a method is void, return statements (§15.9.4) in

that method?s body are not permitted

to specify an expression. If execution of the method body of a void method

completes normally (that is, control

flows off the end of the method body), that method simply returns to its

caller.

When the return type of a method is not void, each return statement in that

method body must specify an

expression of a type that is implicitly convertible to the return type. The

endpoint of the method body of a valuereturning

method must not be reachable. In other words, in a value-returning method,

control is not permitted to

flow off the end of the method body.

[Example: In the example

class A

{

public int F() {} // Error, return value required

public int G() {

return 1;

}

public int H(bool b) {

if (b) {

return 1;

}

else {

return 0;

}

}

}

the value-returning F method results in a compile-time error because

control can flow off the end of the method

body. The G and H methods are correct because all possible execution paths

end in a return statement that

specifies a return value. end example]

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航