<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    最愛Java

    書山有路勤為徑,學海無涯苦作舟

    《AspectJ Cookbook》讀書筆記十六: 增強類和編譯器

            本章節說明了如何使用靜態橫切技術,以靜態方式使用AspectJ中的方面把行為和接口引入現有的類中。使用這些技術,可以擴展類來實現接口,從新的父類擴展類,引入新方法和屬性,減輕說發生異常的影響,以及繼承多個基類。
    一.擴展現有的類
    package com.aspectj;

    public aspect ExtendClassRecipe {
        
    private int MyClass.newVariable = 20;
        
        
    public int MyClass.bar(String name) 
            System.out.println(
    "In bar(String name) , name:" + name);
            
    return this.newVariable; 
        }

    }

     
            示例中將屬性newVariable和方法bar(String)添加到了MyClass類中。

    二.聲明類之間的繼承關系
        使用declare parents語句,指定特定的類是從另一個類擴展而來。
        以下代碼說明了如何為MyClass類指定新的繼承關系
    package com.aspectj;

    public aspect IntroduceInheritanceRecipe {
        declare parents:MyClass 
    extends AnotherClass;
    }

    三.使用方面實現接口
        使用declare parents語句,指定特定的類實現特定的接口。
    package com.aspectj;

    public aspect ImplementInterfaceRecipe {
        declare parents:MyClass 
    implements MyInterface;
    }
        把接口應用于現有類的能力允許通過接口類型的引用那個類的對象,如:
    //Create an instance of MyClass
    MyInterface myObject = new MyClass();
    //
    //Work with the interface reference
    myObject.foo(1,"Russ");

    四.聲明默認的接口實現
    package com.aspectj;

    public aspect DefaultInterfaceImplementationRecipe {
        declare parents:MyClass 
    implements MyInterface;

        
    //public void MyInterface.bar(String name) {
            
    //System.out.println("bar(String) called on " + this);
        
    //}
    }

    五.減輕異常的影響
        使用declare soft語句,可以指定一組應該減輕其影響的異常--也就是說,在通過特定連接點選擇的連接點上引發這些異常時,將其轉換成未捕獲的異常。
        示例中說明了減輕在void foo()方法上引發的ExcepionA異常的影響,使得該方法的用戶不必關心如何處理這個異常。
    package com.aspectj;

    public aspect SoftExceptionRecipe {
        pointcut callPointCut() : call(
    void MyClass.foo());
        declare soft : ExceptionA : callPointCut();
    }

    六.擴展編譯
    分別使用declare error或declare warning語句,指定應該引發編譯器錯誤或警告的條件。
    示例說明了如何聲明一個新的錯誤和警告,如果在正在編譯的應用程序內發現指定的條件,編譯器就會引發該錯誤或警告。
    package com.aspectj;

    public aspect CompilaionAdviceRecipe {
        declare error:call(
    void ProtectedAccessClass.setValue(int))  : "Must only set the ProtectedAccessClass.value from a MyClass object";
        
        declare warning:call(
    void ProtectedAccessClass.getValue()) :"Should only be reading ProtectedAccessClass.value from a MyClass object";
    }

    posted on 2008-08-27 09:31 Brian 閱讀(468) 評論(0)  編輯  收藏 所屬分類: 《AspectJ Cookbook》讀書筆記

    公告


    導航

    <2008年8月>
    272829303112
    3456789
    10111213141516
    17181920212223
    24252627282930
    31123456

    統計

    常用鏈接

    留言簿(4)

    隨筆分類

    隨筆檔案

    收藏夾

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 久久精品国产亚洲AV电影| 国产一区二区三区免费在线观看| 亚洲午夜精品第一区二区8050| 亚洲精品伦理熟女国产一区二区| 2021国产精品成人免费视频| 久久亚洲精品国产精品| 免费成人高清在线视频| 国产V亚洲V天堂A无码| 91成人免费福利网站在线| 亚洲美女在线国产| 日韩在线视频免费| 亚洲高清无码专区视频| 美女被免费视频网站a| mm1313亚洲精品国产| 一个人看的免费视频www在线高清动漫| 亚洲国产成人a精品不卡在线| 一级黄色片免费观看| 亚洲精品无码永久在线观看你懂的| 在线免费观看h片| 4480yy私人影院亚洲| 好吊妞在线成人免费| 国产偷国产偷亚洲高清人| 区三区激情福利综合中文字幕在线一区亚洲视频1 | 韩国免费A级毛片久久| 亚洲日本乱码在线观看| 久草免费手机视频| 亚洲国产成人精品无码一区二区 | 亚洲色偷偷色噜噜狠狠99网| 日韩免费一区二区三区| 人成午夜免费大片在线观看| 亚洲阿v天堂在线| 曰批视频免费40分钟试看天天| 亚洲精品国产国语| 免费一级毛片清高播放| 精品一区二区三区免费| 亚洲一区在线观看视频| 亚洲第一区精品观看| 亚洲视频在线免费看| 最好2018中文免费视频| 亚洲视频在线免费观看| 日本无吗免费一二区|