Hus*_*ri' 11 java constructor return
据我所知,构造函数什么也没有返回,甚至没有返回,
并且
return ;
Run Code Online (Sandbox Code Playgroud)
在任何方法内部意味着返回void.
所以在我的程序中
public class returnTest {
public static void main(String[] args) {
returnTest obj = new returnTest();
System.out.println("here1");
}
public returnTest ()
{
System.out.println("here2");
return ;
}
}
Run Code Online (Sandbox Code Playgroud)
我在打电话
return;
Run Code Online (Sandbox Code Playgroud)
这将返回VOID,但构造函数不应该返回任何东西,程序编译得很好.
请解释 .
Joh*_*136 22
return
在构造函数中,只是在指定点跳出构造函数.如果在某些情况下不需要完全初始化类,则可以使用它.
例如
// A real life example
class MyDate
{
// Create a date structure from a day of the year (1..366)
MyDate(int dayOfTheYear, int year)
{
if (dayOfTheYear < 1 || dayOfTheYear > 366)
{
mDateValid = false;
return;
}
if (dayOfTheYear == 366 && !isLeapYear(year))
{
mDateValid = false;
return;
}
// Continue converting dayOfTheYear to a dd/mm.
// ...
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15481 次 |
最近记录: |