JAVA 中的连续.操作

JAVA代码中经常看到对一个对象的连续.操作,比如:

StringBuilder result = new StringBuilder("");
result.append("line1").append("\n").append("line2").append("\n");

实际上这种操作并非JAVA特有的操作,能否连续进行调用,还需要看该方法的返回值,如果返回对象自身或者新的对象就可以进行相应的操作。

 

如:

class DemoMethod{
 DemoMethod a() {return this;};
 DemoMethod b() {return this;};
 DemoMethod c() {return this;};
 DemoMethod d() {return this;};
}

 

DemoMethod test = new DemoMethod();
    
test.a().b().c().d();

 

也就是说还是要遵循基本的调用规则。

posted @ 2017-08-09 14:24  yunpengli  阅读(915)  评论(0)    收藏  举报