2012-09-25 168 views
-2

可能重複:
Why use getters and setters?使用getter和setter

我已經看到contsructors有一段時間了,我還是不知道如何像這樣的事情的工作,我沒有什麼我知道他們是獲取者和設置者,但是他們在我的代碼中執行的功能正是他們執行的,因爲它似乎只是指自己不執行特定的代碼塊。我只需要一個非常簡單的解釋,不需要爭辯。

public class RSSItem { 

    // All <item> node name 
    String _title; 
    String _link; 
    String _description; 
    String _pubdate; 
    String _guid; 

    // constructor 
    public RSSItem(){ 

    } 

    // constructor with parameters 
    public RSSItem(String title, String link, String description, String pubdate, String guid){ 
     this._title = title; 
     this._link = link; 
     this._description = description; 
     this._pubdate = pubdate; 
     this._guid = guid; 
    } 

    /** 
    * All SET methods 
    * */ 
    public void setTitle(String title){ 
     this._title = title; 
    } 

    public void setLink(String link){ 
     this._link = link; 
    } 

    public void setDescription(String description){ 
     this._description = description; 
    } 

    public void setPubdate(String pubDate){ 
     this._pubdate = pubDate; 
    } 


    public void setGuid(String guid){ 
     this._guid = guid; 
    } 

    /** 
    * All GET methods 
    * */ 
    public String getTitle(){ 
     return this._title; 
    } 

    public String getLink(){ 
     return this._link; 
    } 

    public String getDescription(){ 
     return this._description; 
    } 

    public String getPubdate(){ 
     return this._pubdate; 
    } 

    public String getGuid(){ 
     return this._guid; 
    } 
} 
+0

看到http://stackoverflow.com/questions/12098636/what-is-the-use-of-getter-and-setter-method/12098728#12098728和http: //stackoverflow.com/questions/1568091/why-use-getters-and-setters –

+1

它的基本概念。看看http://en.wikipedia。org/wiki/JavaBeans – ManojGumber

+5

當你在類中聲明變量爲「private」時,getter和setter會更有意義,然後在任何需要getter和setter的地方訪問 – AurA

回答

2

構造函數在一個原子操作中執行對象的初始化。當你調用構造函數時,返回時你有一個完全創建的對象。將它與一個簡單的無參數構造函數進行比較,然後是一連串的setter。在這種情況下,您可以輕鬆地創建一個對象不完整。

您是否應該使用智能構造函數來完成參數並完全構建對象,而不是使用一系列setter?一般來說,是的。這裏的原因:

  1. 操作原子,並會給你一個完整的,正確的對象(我假設你驗證輸入)
  2. 可以提供替代來創建領域,串/流等對象
  3. 通過使用final字段,您可以創建不可變的對象。這對確定可靠性(特別是線程)和調試問題非常有用。

一般來說,我將setter/getters視爲可憐的OO設計。在他們最基本的情況下,他們只是暴露內部領域你可以提供驗證等,但你仍然可能暴露實現。我寧願使用構造函數實例化對象,然後讓它使用定義好的方法爲我做事,而不是通過getters將數據拉出來,然後自己做。這是面向對象的總體目標 - 告訴對象爲你做事情,而不是要求他們自己做數據並自己動手。

0

如果以上面提到的方式調用構造函數,則需要設置變量的值;所以你創建的setter方法沒有意義。如果你想使你的setter方法有用,創建一個空的構造函數,然後使用這些函數。

0

GetterSetter是一種實現面向對象的原理Encapsulation的方法。

-Encapsulation以其最基本的形式類似於private fieldspublic Getter and Setters

-最重要的原因是使用Getter and Setter實現驗證傳遞給fields數據

例如:

在下面的例子沒有獲取和設置的,所以狗的重量可以被設置爲-10

public class Dog{ 


    int weight; 

    public static void main(String[] args){ 

        new Dog().weight = -10; 

      } 


} 

例如無效值:

我現在使用Setter和Getter來驗證現場的重量

public class Dog{ 


     int weight; 

    public void setWeight(int i){ 

    if(i < 0){      // Validation 

      System.out.println("Invalid weight"); 

     }    

    else{ 
     this.weight = i; 

    } 

} 


    public int getWeight(){ 

     return this.weight; 

} 

     public static void main(String[] args){ 

         new Dog().setWeight(50); 

       } 


    } 
+4

你應該重新格式化該代碼。縮進真的很糟糕。我認爲你過度使用** bold **和'code blocks'。 – maba

+0

@maba對不起,如果縮進不是那麼好,但正如我從我的手機輸入這個,我幾乎沒有任何選擇...... –

+1

「封裝的最基本的形式就像有私人領域與公共Getter和Setter 」。我認爲封裝的最基本形式是*隱藏*實現。因爲這樣的基本設置者/獲取者似乎違反了這個原則 –

0

在這種情況下,你不需要getter/setter方法,但它仍然是一件好事,使用它們。如果稍後注意到您想在設置時檢查邊界。或者當你得到s.th.你可能想要執行一些數據轉換。

另一個簡單的例子:你只是想在構造元素期間設置一個值,所以你只是不實現一個setter,但你實現了一個getter(以及,總是將這些成員變量設置爲私有的,否則Setters /吸氣者是毫無意義的)。

所以在大多數情況下,你可能不會需要他們,但它仍然是一個好主意,始終使用塞特斯/吸氣劑(IMO)

0

你參數的構造函數初始化時你的成員變量

  • 字符串_title;
  • String _link;
  • String _description;
  • String _pubdate;
  • String _guid;

getter和setter的作用是讀取和寫入這些成員變量的值。它們是更多的標準,因爲它被廣泛用於許多基於Java的框架作品。

0

獲取者和設置者提供對訪問變量的保護。您可以針對不同的用戶組使用具有不同可見性級別的getter和setter,並在代碼中提供更大的靈活性。這並不意味着每個變量都需要一個getter和setter。想想他們可以提供的訪問級別以及可以構建到各自訪問級別的功能。這是一個名爲Sample類:

public class Sample { 
    public static final int DEFAULT_QTY = 8; 

    /**Create object with qty = 0*/ 
    public Sample(){ 
     setQty(DEFAULT_QTY); 
     //this.qty = DEFAULT_QTY;//here makes no difference 
    } 

    /**Create object with qty if qty > 0*/ 
    public Sample(int qty){//drops confusion created by this.qty vs qty 
     //provides built-in range protection through the constructor's use of setter 
     setQty(qty); 
    } 

    /**@param qty The qty to set for this object, must be > 0*/ 
    public void setQty(int qty){ 
    if(qty > 0) { 
     this.qty = qty; 
    else { 
     informUser(QTY_MUST_BE_>_0); 
     //setQty(0); or getValidInput(); 
     } 
    } 

    /*@param forSureQty The pre-verified value to set for qty*/ 
    protected void setQtyNow(int forSureQty) { 
     this.qty = forSureQty; 
    } 

    /**@return Returns the qty if qty < qtyAvailable, returns -1 otherwise*/ 
    public int getQty(){ 
     //Avoid problems with the computer selling more than you have available 
     if(getQtyAvailable < this.qty) { 
     //informUser(QTY_AVAILABLE = x QTY_NEEDED = >x); 
     return -1; 
     } 
     return this.qty; 
    } 

    /*@return Returns the value of qty for this object*/ 
    protected getQtyNow() 
     return this.qty; 
    } 

    private int qty; 

}