軟件工程實踐者

          導航

          <2011年2月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272812345
          6789101112

          隨筆分類

          文章分類

          隨筆檔案

          統計

          留言簿

          友情鏈結

          閱讀排行榜

          評論排行榜

          2011年2月23日 #

          Java 8 Repeatable注解的使用

          Java 8之前,同一注解不能在相同的目標元素上多次使用,例如,如下的注解在Java 8之前是不允許的:
          public class SampleClass {
              
              @Quality("Security")
              @Quality("Performance")
              @Quality("Readability")
              public void foo(){
                  //
              }
          }

          Java 8引入了Repeatable注解(@Repeatable)可以解決這一問題,但光有可重復的注解定義還不夠,還需要它的容器注解,兩者一起來實現可重復注解的使用。實例如下:
          @Target(ElementType.METHOD)
          @Retention(RetentionPolicy.SOURCE)
          @Repeatable (Qualities.class)
          public @interface Quality {
              String value();
          }
          @Target(ElementType.METHOD)
          public @interface Qualities {
              Quality[] value();
          }

          其中,Quality是可重復注解,由@Repeatable注解標明,它的容器注解是Qualities,用于存放所有可重復的Quality(存貯在Quality[]中);同時還要注意可重復注解和它的容器注解的目標元素必須是一樣的(這也不言自明)。如此這般,我們最開始的 SampleClass 在Java 8環境下就可以安全使用了。


          posted @ 2016-09-06 16:24 軟件工程實踐者 閱讀(833) | 評論 (0)編輯 收藏

          Bill Push的單例模式實現

          以下單例實現思想來自《Java Design Patterns: A Programmer's Approach》.
          該方法利用了Java缺省的Lazy類實例化機制克服了傳統單例模式實現中Lazy實例化方式的不足。

          public class Singleton {
             private Singleton(){}
             
             public static Singleton getInstance(){
                return Helper.instance;
             }

             static class Helper {
                 private static Singleton instance = new Singleton();
             }
          }

          posted @ 2016-09-03 09:37 軟件工程實踐者 閱讀(230) | 評論 (0)編輯 收藏

          Maven error “Failure to transfer…”

          以下轉自StackOverflow(http://stackoverflow.com/questions/5074063/maven-error-failure-to-transfer),親測可用。


          This worked for me in Windows as well.

          1. Locate the {user}/.m2/repository (Using Juno /Win7 here)
          2. In the Search field in upper right of window, type ".lastupdated". Windows will look through all subfolders for these files in the directory. (I did not look through cache.)
          3. Remove them by Right-click > Delete (I kept all of the lastupdated.properties).
          4. Then go back into Eclipse, Right-click on the project and select Maven > Update Project. I selected to "Force Update of Snapshots/Releases". Click Ok and the dependencies finally resolved correctly.

          posted @ 2016-09-02 15:52 軟件工程實踐者 閱讀(183) | 評論 (0)編輯 收藏

          Groovy:Invalid duplicate class definition of class....The type xxx is already defined..

          當我們寫Groovy腳本代碼的時候,有時會發生編譯錯誤,如下:

          - Groovy:Invalid duplicate class definition of class XXX : The source XXXX\XXX.groovy contains at least two 
          definitions of the class XXX.
          - The type XXX is already defined

          原因在于Groovy會把.groovy代碼文件作為腳本或類定義來處理,例如如下代碼:

          class Order {
              def security
              def value
              private buy_sell(su, closure) {
                  security = su[0]
                  quantity = su[1]
                  closure()
              }
              def getTo() {
                  this
              }
          }
          def methodMissing(String name, args) {
              order.metaClass.getMetaProperty(name).setProperty(order, args)
          }
          def getNewOrder() {
              order = new Order()
          }

          Integer.metaClass.getShares = { -> delegate }

          Groovy會把上述代碼作為腳本處理,同時缺省用文件名來作為一個外圍類類包括整個腳本程序,此時,如果該文件名恰好也是Order的話,那么就會出現重復的類定義錯誤提示。
          解決辦法是將腳本文件名取另外一個不同的名字。

          posted @ 2016-08-31 09:00 軟件工程實踐者 閱讀(1071) | 評論 (0)編輯 收藏

          OMG OCUP2考試通知

          已經申請OCUP中級考試的學員可以在一年內(截止到17年9月份)免費申請OCUP2中級考試的資格(原有考試仍可以參加)。此外,2014年3月份之后參加了原有OCUP中級認證考試的學員可以免費申請OCUP2中級認證考試。詳見OMG網站聲明(http://www.omg.org/ocup-2/exam-info.htm)。

          posted @ 2016-08-30 14:10 軟件工程實踐者 閱讀(293) | 評論 (0)編輯 收藏

          博客搬家

          搬家總是難免的,但舊家的東西不能帶走難免會留下些許遺憾,希望它們能永遠留下來.......
          歡迎光臨我的新家:http://blog.sciencenet.cn/?53016 (科學網)

          posted @ 2011-02-23 11:38 軟件工程實踐者 閱讀(191) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 分宜县| 邮箱| 新安县| 舒兰市| 崇明县| 微山县| 明光市| 寿阳县| 沐川县| 侯马市| 衡南县| 梨树县| 化州市| 平山县| 二连浩特市| 井研县| 杭锦后旗| 长海县| 乌拉特中旗| 昌都县| 邯郸市| 宜川县| 榕江县| 和平县| 砀山县| 阿尔山市| 白银市| 读书| 开阳县| 毕节市| 庆元县| 寿阳县| 江油市| 关岭| 扎囊县| 石楼县| 奉化市| 临沧市| 和平区| 阿荣旗| 满城县|