日期:2014-05-20  浏览次数:20637 次

关于方法
public   class   Leaf   {
int   i=0;
Leaf   inc()
{
i++;
return   this;
}
void   print()
{
System.out.println( "i= "+i);
}

public   static   void   main(String[]   args)   {
Leaf   x=   new   Leaf();
x.inc().inc().inc().print();
}
}
这个程序中   Leaf   inc()
{
i++;
return   this;
}
什么意识阿?   方法体的话不是需要声明返回类型吗,可这个是(类名   方法名)
应该怎么理解阿   ?

------解决方案--------------------
返回Leaf型对像.
------解决方案--------------------
返回类leaf型的对象
如:x.inc()返回的就是leaf对象,所以它可以有x.inc().inc()的方法,
要不然就没办法用x.inc().inc().inc().print();了