Java--返回类的对象(return this)

如下代码所示:

1 public Book getBook(){
2     return this;        
3 }

在getBook()方法中,方法的返回值为Book类,所以方法体中使用 return this 这种形式返回一个Book类的对象。

而函数返回对象有什么好处呢

当函数的返回值返回一个该类的对象时,就可以用这个函数的返回值继续调用该类或其子类的属性了

如:

getBook().getBook().getBook()
原文地址:https://www.cnblogs.com/brillant-ordinary/p/10154566.html