玩轉Eclipse — 自動產生setter和getter方法

來源:互聯網
上載者:User

        我們在程式開發過程中,往往要編寫這樣的類:類的部分或者全部屬性不希望讓外部世界直接存取,而不用public欄位修飾。這樣,方法調用成了訪問這些屬性的唯一途徑。JavaBean就是一個很好的例子,其嚴格遵守物件導向的設計邏輯,所有屬性都是private。對於任何屬性xxx,都有public的getXxx()方法來擷取屬性和public的setXxx()方法來修改屬性。如果只有少量這樣的屬性,可以通過手動方式為它們添加setter和getter方法。但是,如果有大量這樣的屬性,手動添加會很費時。

        下面通過一個樣本,來介紹如何通過Eclipse自動產生需要的setter和getter方法。範例程式碼如下:

/** * The Class Boy. */public class Boy {        /** The name. */    private String name;        /** The age. */    private int age;        /** The smart. */    private boolean smart;}
1.  基本設定

        在代碼編輯器中開啟Boy.class檔案,使用快速鍵Alt + Shift + S,再按R鍵(你Eclipse中的快速鍵可能不同),或者右鍵選擇Source -> Generate Getters and Setters...,操作如下圖所示:


        進入自動產生setter和getter方法的設定介面如下:


Select

Select All:選擇為所有的屬性添加setter和getter方法

Deselect All:取消所有已選擇的setter和getter方法

Select Getters:選擇所有屬性的getter方法

Select Setters:選擇所有屬性的setter方法 Insertion point

可以選擇為該檔案的“First Member”,“Last Member”,或者某個元素之後等。

Sort by

Fields in getter/setter pairs:每個屬性的getter和setter方法成對排序

First getters, then setters:所有的getter方法在所有的setter方法之前 Access modifier

可以選擇存取權限:public,protected,default,private

還可以選擇是否為final或者synchronized Comments

可以選擇是否在自動產生setter和getter方法的同時,為它們產生注釋

       另外,在Code Template中可以設定自動產生的setter和getter方法的主體和注釋的格式。 2. setter方法的參數加首碼

        一般情況下,自動產生的setter方法中的參數,會跟屬性完全相同,需要通過this來區分同名屬性和參數。樣本如下:

    /**     * @param age the age to set     */    public final void setAge(int age) {        this.age = age;    }

        在比較嚴的代碼格式檢查中,這種情況會提示‘xxx’ hides a field的問題。為了避免這種checkstyle的問題,通過在該項目的.settings目錄下的org.eclipse.jdt.core.prefs檔案,在其末尾添加org.eclipse.jdt.core.codeComplete.argumentPrefixes=new,就可以在自動建立的所有setter方法的參數前面加上new首碼。這種配置,需要重新啟動Eclipse才會生效。這種方法的具體操作和分析,可以參考《玩轉Eclipse — 項目的.settings目錄解密》。

        進行以上配置之後,自動產生的setter和getter方法後的完成的代碼如下:

/** * The Class Boy. */public class Boy {        /** The name. */    private String name;        /** The age. */    private int age;        /** The smart. */    private boolean smart;    /**     * @return the name     */    public final String getName() {        return name;    }    /**     * @param newName the name to set     */    public final void setName(String newName) {        name = newName;    }    /**     * @return the age     */    public final int getAge() {        return age;    }    /**     * @param newAge the age to set     */    public final void setAge(int newAge) {        age = newAge;    }    /**     * @return the smart     */    public final boolean isSmart() {        return smart;    }    /**     * @param newSmart the smart to set     */    public final void setSmart(boolean newSmart) {        smart = newSmart;    }}
        說明:

                1)setter方法的參數會自動大寫屬性的首字母,並加上首碼。

                2)setter方法的參數在屬性前面加了首碼之後,就沒有必要再用this區分屬性和參數。

                3)對於boolean類型的屬性,getter不再是get開頭,而是以is開頭。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.