天天看點

使用IntelliJ IDEA自動生成成員對象的代理方法

在《Thinking in Java 4th》7.3 代理 中 作者提到可以使用IntelliJ IDEA自動生成成員對象代理方法的代碼。找了半天才找到怎麼操作。

例子:

package aaa;

public class A {

    private B b;


}

class B {
    public void b1() {
    }

    public void b2() {
    }

    public void b3() {
    }
}





           

我們在A中要生成B中三個方法的代理方法。

操作步驟:在類A的區域中 點右鍵 注意:在哪個類中生成就在哪個類的區域内操作。

使用IntelliJ IDEA自動生成成員對象的代理方法
使用IntelliJ IDEA自動生成成員對象的代理方法
使用IntelliJ IDEA自動生成成員對象的代理方法
使用IntelliJ IDEA自動生成成員對象的代理方法
使用IntelliJ IDEA自動生成成員對象的代理方法

生成以後的代碼:

package aaa;

public class A {

    private B b;

    public void b1() {
        b.b1();
    }

    public void b2() {
        b.b2();
    }

    public void b3() {
        b.b3();
    }
}

class B {
    public void b1() {
    }

    public void b2() {
    }

    public void b3() {
    }
}




           

喜歡就點個贊哈

繼續閱讀