2015-02-08 96 views
1

我試圖修改「設置」部分,但這些更改似乎根本沒有發生。下面是顯示了相同的結果基本代碼:獲取和設置屬性似乎被忽略(C#)

class Class1 
{ 
    private int num; 

    public Class1(int number) 
    { 
     num = number; 
    } 
    public int getNumber 
    { 
     get 
     { 
      return num; 
     } 
     set 
     { 
      if (value > 0) 
       num = value; 
      else 
       num = 0; 
     } 
    } 
} 

在這裏,我想作任何負值的0

class Program 
{ 
    static void Main(string[] args) 
    { 
     Class1 c1 = new Class1(10); 
     Class1 c2 = new Class1(-10); 

     Console.WriteLine(c1.getNumber); 
     Console.WriteLine(c2.getNumber); 
     Console.ReadLine(); 
    } 
} 

結果給我

10 -10

我試過使用

 set 
     { 
      num = 100; 
     } 

但結果仍然沒有變化。我試着用我正在使用的書進行雙重檢查,並且沒有區別,我可以看到。如果它意味着什麼,我正在使用Visual Studio 2012。

回答

2

你的代碼是不是要求集您財產的一部分。因爲,你只是在調用構造函數。而在你的構造函數中,你只是爲後臺變量設置臨時值(num)。
另外,by convention,您的班級成員名稱不合適。 將其更改爲:

NUM - >數
getNumber - >數

試試這個:

Class1 c1 = new Class1(); 
c1.Number = -10; // The set accessor is invoked here 
int myNumber = c1.Number; // The get accessor is invoked here 


如果你想通過你的構造函數來調用set訪問,然後更改您的構造函數爲:

public Class1(int number) 
{ 
     Number = number; 
} 

然後,它會調用適當的set訪問:

Class1 c1 = new Class1(10); // The set accessor will be invoked 


不要忘記改變你的類實現爲:

class Class1 
{ 
    private int number; 

    public int Number 
    { 
     get { return number; } 
     set 
     { 
      if (value > 0) 
       number = value; 
      else 
       number = 0; 
     } 
    } 

    // If you do provide a constructor (any constructor with any signature), 
    // the parametrless constructor will not be generated 
    public Class1() 
    { 
    } 

    public Class1(int number) 
    { 
     Number = number; 
    } 
} 

this from msdn瞭解更多信息。

+0

啊我現在看到了。謝謝!你能告訴我爲什麼應該按照這種方式組織班級實施嗎? – Alex 2015-02-08 18:50:52

+0

@Alex因爲,這是慣例。編碼約定是針對特定編程語言的一組準則。強烈建議軟件程序員遵循這些指導原則,以幫助提高其源代碼的可讀性並使軟件維護更爲簡單。我建議閱讀[this](http://en.wikipedia.org/wiki/Coding_conventions)。 – 2015-02-08 19:10:16

2

試試這個:

class Class1 
{ 
    private int num; 

    public Class1(int number) 
    { 
     Number = number; 
    } 
    public int Number 
    { 
     get 
     { 
      return num; 
     } 
     set 
     { 
      if (value > 0) 
       num = value; 
      else 
       num = 0; 
     } 
    } 
} 

你沒有正確implented它。其實更好的implmentation是以下幾點:

class Class1 
{ 
    // The backing field has the same name as the Property 
    // but all letters must be lowercase. 
    private int number; 

    public int Number 
    { 
     get { return number; } 
     set 
     { 
      if (value > 0) 
       number = value; 
      else 
       number = 0; 
     } 
    } 

    // In the constructor we set the value of the backing fields 
    // using the corresponding properties. 
    public Class1(int number) 
    { 
     Number = number; 
    } 
} 

將要設置的number價值或得到它的使用相應的屬性值每次:

// Set the value 2 to the number 
Number = 2; 

// Read the value stored in number and assigned to value. 
var value = Number; 
+0

可以請downvoter解釋我以上哪裏錯了?謝謝 – Christos 2015-02-08 08:59:52