gpt4 book ai didi

java - 在Java中,return语句可以出现在什么地方?

转载 作者:搜寻专家 更新时间:2023-11-01 04:03:38 25 4
gpt4 key购买 nike

除了在方法/构造函数体内?

此外,构造函数可以返回一些东西吗?还是我可以只使用“return;”,后面没有任何表达式?

最佳答案

In Java, where can a return statement appear? Besides inside a method/constructor body?

仅在构造函数或方法中:

JLS-http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#6767

A return statement returns control to the invoker of a method (§8.4, §15.12) or constructor (§8.8, §15.9).


Also, is there something that can be returned by a constructor?

没有

JLS-http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#6767

A return statement with an Expression must be contained in a method declaration that is declared to return a value (§8.4) or a compile-time error occurs.


Or can I only use "return;" without any Expression following it?

JLS-http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#6767

A return statement with no Expression must be contained in the body of a method that is declared, using the keyword void, not to return any value (§8.4), or in the body of a constructor (§8.8)

关于java - 在Java中,return语句可以出现在什么地方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4855029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com