当前位置: 代码迷 >> J2SE >> 关于方法解决思路
  详细解决方案

关于方法解决思路

热度:76   发布时间:2016-04-24 14:41:53.0
关于方法
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();了
  相关解决方案