說法我試圖重構下面的代碼傳遞函數爲JDK 8
class FileDownloadResource{
@Inject
private FileDownload fileDownload;
@Path(/abc)
public Response downloadFileABC(){
try{
fileDownload.downloadABC();
}catch(IOException e){
}
//Some code here that is common to the second method as well
}
@Path(/xyz)
public Response downloadFileXYZ(){
try{
fileDownload.downloadXYZ();
}catch(IOException e){
//handle exception
}
//Some code here that is common to the first method as well
}
}
類是JAX-RS REST資源。正如你在上面的代碼中看到的那樣,除了try塊中的內容外,對於兩種方法來說都是一樣的。我們可以使用任何新的JDK 8 功能將fileDownload.downloadABC()
作爲參數傳遞給私有方法嗎?
基本上,我正在尋找一些方法來傳遞函數 作爲參數,並讓代碼的其他部分相同。
即使* try-block中的代碼是相同的。 – Andreas
@Andreas修正了 – user2434
是的,我的答案是這樣認爲的。 Tagir的答案與我的相似,儘管他使用功能界面的名字可能更好些,但我同時展示了使用它的ABC和XYZ版本。 – Andreas