def antBuilder = new AntBuilder()
antBuilder.tar(basedir: getOutputDirectory(),  destfile: getTarFile())
antBuilder.gzip(src: getTarFile(), destfile: getTarGzipFile())
antBuilder.delete(file: getTarFile())
antBuilder.delete(dir: getOutputDirectory(), includes:"*.xml")


我有做上述的方法。如您所见,getTarFile()方法总体上称为三次。使用getTarFile()三次或定义一个局部变量来保存getTarFile()的值并改为使用它的首选编码方式是什么?

最佳答案

您可以真正获得想要的Groovy ...

您可以(例如)将其包装在闭包中:

def doStuff = { tar, out, gzip ->
  new AntBuilder().with {
    tar( basedir: out,  destfile: tar )
    gzip( src: tar, destfile: gzip )
    delete( file: tar )
    delete( dir: out, includes:"*.xml" )
  }
}


并致电:

doStuff( getTarFile(), getOutputDirectory(), getTarGzipFile() )


无论采用哪种方式,除非getTarFile进行大量工作(这似乎不太可能),否则您获得的收益将不会超过可读性...

虽然那本身就是一个胜利;-)

关于java - 有优化建议吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7973675/

10-12 05:38