2015-06-19 35 views
0

我只是在玩C#,而我正在自我調整,正確的方法是Getter和Setter。我發現這樣的谷歌:在C中獲取和安裝程序#

class MyClass 
{ 
    Button btnMyButton; 

    // some code... 

    public Button getBtnMyButton 
    { 
     get 
     { 
      return btnMyButton; 
     } 
    } 
} 

有沒有一個'適當'的方式?或者,這也是好的:

class MyClass 
{ 
    Button btnMyButton; 

    // some code... 

    public Button getBtnMyButton() 
    { 
     return this.btnMyButton; 
    } 
} 

有什麼區別?

+1

這是完全一樣的事情 –

+0

我建議你在C性能上宣讀了#https://msdn.microsoft.com/en-us/library/w86s7x04.aspx – Jeremy

+2

一些人認爲,前者是正確的,其他人認爲後者是正確的... –

回答

7

正如托馬斯所說,這些都是一樣的東西。您可能想知道gettersetter這個點的含義,它主要是語法糖。但是,這也意味着您不必在後臺爲您創建顯式字段。因此,你可以簡單地做

public Button MyButton { get; private set; } 

private set;確保只有類可以設置它的值,因此它本質上是隻讀到外面類。刪除private將允許外部類寫入該變量。

+1

我只是添加我們通常不會添加「get」前綴。 在C#中,它是屬性而不是方法。我們在沒有「()」的情況下訪問它們。爲前。 MyClass.MyButton而不是MyClass.GetMyButton() – Demonia

+0

非常好的點Demonia,我錯過了,只是複製了原來的名字,謝謝!我已經更新了答案。 – keyboardP

+0

@Demonia謝謝。在最後幾周學習Java,如果我沒有錯,在Java中我被告知使用get和set前綴。 – RagnarLodbrok

0

實際上,getter/setter只不過是一種返回/提供內部值的方法。所以在寫

public Button getBtnMyButton 
{ 
    get 
    { 
     return btnMyButton; 
    } 
} 

你實際上寫一個getter,方法與此類似:

public Button getBtnMyButton 
{ 
    return this.btnMyButton; 
} 

所以在Java中類似的方法是使用方法。

0

他們基本上是一樣的東西。 this指針指向正在調用該方法的對象。

關於這個指針有趣的事情是,它可以讓你寫set功能,如

public void setMyButton (Button myButton) 
{ 
    this.myButton = myButton; 
} 

(不知道這是有效的C#,因爲我從來沒有與它的工作,但你的想法。這應該在Java/C++中工作)

2

您通常不會爲屬性添加get/set前綴。 就寫這樣的:

private Button myButton; 

public Button MyButton{ 
    get{ 
     return myButton; 
    } 
    /*set{ 
     myButton = value; 
    }*/ 
} 

是的,這意味着在你的背景是相同的。該this.會在這種情況下需要:
(注:這是一個愚蠢例如,只應該表現出你的想法)

private Button myButton; 

public Button MyButton{ 
    get{ 
     Button myButton = null; 
     return this.myButton; //<- this. is required or you would end up getting null all the time. 
    } 
    /*set{ 
     myButton = value; 
    }*/ 
} 

編輯:
添加的get/set來自諸如C++或Java之類的語言,其中沒有豪華的屬性。使用get/set表示(重)操作。開發人員可能會考慮緩存結果,而不是多次調用它。
僅在要指定(重)操作的方法上使用get/set。如果它是一個非常(簡單)的操作,你甚至可能最終使用屬性而不是方法。在Intellisense(Visual Studio)中,屬性就像一個字段一樣呈現,因此我們可以假設沒有任何操作正在進行。因此我將(通常)永遠不會緩存屬性的結果。

另一方面 - 如果我找到一個名爲GetResultOfImposible的屬性。
然後,我會propably決定緩存。
名爲ResultOfImposible的屬性聽起來不那麼沉重,我不會緩存它。
(也許我會找到一個性能高峯後改變主意)

1

你應該多想一下屬性的命名,因爲一個屬性可以同時具有getter和setter。考慮以下內容:

public class MyClass 
{ 
    private Button btnMyButton; 

    public Button getMyButton 
    { 
     get{ return btnMyButton; } 
     set{ btnMyButton = value; } 
    } 
} 

    // in some other class 
    void ChangeActiveButton(Button newButton) 
    { 
     MyClass theThing = GetTheThing(); 

     // This doesn't read well... 
     theThing.getMyButton = newButton; 
    } 

當您實現屬性獲取器和設置器時,不要在名稱前加'get'和set'。 (對於許多開發人員來說,方法或函數中的'get'和'set'這兩個詞意味着代碼必須停止工作並完成獲取或設置,而不是簡單地返回或分配一個已經存在的值手)。

public class MyClass 
{ 
    private Button btnMyButton; 

    // Note that the property just has a name, no prefix. 
    public Button MyButton 
    { 
     get{ return btnMyButton; } 
     set{ btnMyButton = value; } 
    } 
} 

另外請注意,你可以讓財產的getter和setter private即使物業本身是類暴露在外。

public Button MyButton 
{ 
    get{ return btnMyButton; } 
    private set{ if(null == btnMyButton) btnMyButton = value; } 
} 

這提供了MyClass與priveliged訪問setter,它可以被用來實現任何特定的財產分配規則。

您也可以使用Auto-Implemented Properties,不帶額外的成員變量。

public Button MyButton { get; private set; } 

c#中的屬性很棒。明智地使用它們,它將幫助您創建更好的結構化,更易於維護的代碼。

0

區別在於目的。你應該使用屬性,當代碼只返回一些邏輯很少或根本沒有它的值。另外一般來說,如果沒有調用setter,值應該是相同的。當創建值(不存儲)或邏輯複雜時,應該使用方法。創建自我記錄的代碼是一種很好的做法。

0

不同之處在於:

public Button getBtnMyButton() 
{ 
    return this.btnMyButton; 
} 

方法,它可以接受輸入參數和返回的輸出參數。

另:

public Button getBtnMyButton 
{ 
    get 
    { 
     return btnMyButton; 
    } 
} 

屬性。你可以看到一個propery作爲一個變量的「包裝器」,它允許你驗證變量值並執行其他類型的東西。

例子:

public Button getBtnMyButton 
{ 
    get 
    { 
     if (btnMyButton != null) 
      return btnMyButton; 

     throw new Exception("Button is null!"); 
    } 
    set 
    { 
     if (value == null) 
      return; 

     btnMyButton = value; 
    } 
}