亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 1924 | 回復(fù): 0
打印 上一主題 下一主題

使用 Lombok 簡(jiǎn)化項(xiàng)目中無(wú)謂的Java代碼 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2015-07-10 13:30 |只看該作者 |倒序?yàn)g覽
在寫(xiě)使用Java時(shí),難免會(huì)有一些模板代碼要寫(xiě),不然get/set,toString, hashCode, close 資源,定義構(gòu)造函數(shù)等等。代碼會(huì)顯得很冗余,很長(zhǎng)。Lombok項(xiàng)目可以是我們擺脫這些東西,通過(guò)一系列的注解,Lombok可以幫我們自動(dòng)生成這些函數(shù)。

Lombok 官網(wǎng)地址:https://projectlombok.org/

參考文檔:https://projectlombok.org/features/index.html

1. 安裝

到官網(wǎng)下載 lombok.jar,直接雙擊,按照提示進(jìn)行操作,就可以在eclipse中安裝成功。

如果使用maven時(shí),則需要引入依賴:
  1. <dependency>
  2.         <groupId>org.projectlombok</groupId>
  3.         <artifactId>lombok</artifactId>
  4.         <version>1.16.4</version>
  5.         <scope>provided</scope>
  6.     </dependency>
復(fù)制代碼
如果需要用javac或者其他命令工具編譯java類(lèi),則需要將 lombok.jar放入classpath.

2. 使用方法 (文檔:https://projectlombok.org/features/index.html)

1> @Getter/@Setter, 注解在一個(gè)pojo類(lèi)上,會(huì)在編譯時(shí),幫我們自動(dòng)生成get/set函數(shù)。

2> @ToString 注解在類(lèi)上,編譯時(shí),幫我們生成包括所有field的toString函數(shù);

3> @EqualsAndHashCode,  編譯時(shí),幫我們生成equlas 和hashCode函數(shù);

4> @Cleanup, 注解在一些資源對(duì)象的定義上,可以幫我們自動(dòng)調(diào)用它們的close()函數(shù);這個(gè)很有幫助;

5> @NoArgsContructor,@RequireArgsContructor, @AllArgsContructor,分別幫我們生成無(wú)參數(shù)構(gòu)造函數(shù),每一個(gè)非Null的field的構(gòu)造函數(shù),所有field參數(shù)的構(gòu)造函數(shù);

6> @Data,All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and @RequiredArgsConstructor! (等價(jià)于:@ToString, @EqualsAndHashCode, @Getter, @Setter, @RequiredArgsConstructor)

更多的注解,參見(jiàn)https://projectlombok.org/features/index.html

3. 例子
  1. @Data
  2. @AllArgsConstructor
  3. @NoArgsConstructor
  4. public class Test {
  5.     private int id;
  6.     private String name;
  7.     private String password;

  8.     public static void main(String[] args) {
  9.         Test test = new Test(1, "test", "password");
  10.         System.out.println(test);
  11.         System.out.println(test.getName());
  12.     }
  13. }
復(fù)制代碼
結(jié)果:
  1. Test(id=1, name=test, password=password)
  2. test
復(fù)制代碼
通過(guò)@Data, @AllArgsConstructor,@NoArgsConstructor 三個(gè)注解自動(dòng) 生成了 Test 的全field參數(shù)的構(gòu)造函數(shù),自動(dòng)生成了 toString(), get/set函數(shù)等等。

再看一例:
  1. public static void main(String[] args) throws IOException{
  2.         @Cleanup
  3.         InputStream in = new FileInputStream("/home/a.txt");
  4.         @Cleanup
  5.         OutputStream out = new FileOutputStream("/home/b.txt");
  6.         byte[] b = new byte[10000];
  7.         while (true) {
  8.             int r = in.read(b);
  9.             if (r == -1)
  10.                 break;
  11.             out.write(b, 0, r);
  12.         }
  13.     }
復(fù)制代碼
@Cleanup自動(dòng)幫我們調(diào)用 close() 方法進(jìn)行關(guān)閉資源。

You can use @Cleanup to ensure a given resource is automatically cleaned up before the code execution path exits your current scope. You do this by annotating any local variable declaration with the @Cleanup annotation like so:
@Cleanup InputStream in = new FileInputStream("some/file");
As a result, at the end of the scope you're in, in.close() is called. This call is guaranteed to run by way of a try/finally construct.

If the type of object you'd like to cleanup does not have a close() method, but some other no-argument method, you can specify the name of this method like so:
@Cleanup("dispose") org.eclipse.swt.widgets.CoolBar bar = new CoolBar(parent, 0);
By default, the cleanup method is presumed to be close(). A cleanup method that takes 1 or more arguments cannot be called via @Cleanup.

@Cleanup是通過(guò) try/finally 實(shí)現(xiàn)的,如果資源的關(guān)閉方法不是默認(rèn)的close(),那么也可以指定關(guān)閉方法的名稱@Cleanup("closeMethod"), 但是關(guān)閉方法不能有參數(shù),不然就無(wú)法使用 @Cleanup了。

更多的 參考 https://projectlombok.org/features/index.html

通過(guò)使用 Lombok,可以減少很多的 Java 代碼的,減輕了心理負(fù)擔(dān)。
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP