2017-08-10 40 views
-1

在程序中,我在控制檯上寫着一隻狗的品種。從對象中返回字段值的最佳方法

哪個是最好的方法,爲什麼?

我想,第一種方式少做業務,但要做到這一點,我必須聲明的變量爲public。

也許是一個更好的選擇,宣稱它私人和從方法返回的數據,如我在第二的WriteLine做?

我想了解哪一種方法是軟件

class Program 
{ 
    static void Main(string[] args) 
    { 
     Dog fuffy = new Dog("Fuffy", "Armant"); 
     Console.WriteLine(fuffy.breed); 
     Console.WriteLine(fuffy.getBreed()); 
    } 
} 


class Dog 
{ 
    public string name; 
    public string breed; 

    public Dog(string name, string breed) 
    { 
     this.name = name; 
     this.breed = breed; 
    } 

    public string getBreed() 
    { 
     return this.breed; 
    } 
} 

編輯的最佳考慮每一個重要的方面:

有沒有使用getter和這種方法之間的真正區別?

吸氣劑是不是隻是一種「隱藏」的方式來編寫和執行該方法?

與該方法相比,吸氣劑是否能提供更好的預置?

class Dog 
{ 
    public string name { get; } 
    public string breed { get; } 

    public Dog(string name, string breed) 
    { 
     this.name = name; 
     this.breed = breed; 
    } 

    public string getBreed() 
    { 
     return this.breed; 
    } 
} 
+0

你沒有在你的榜樣的方式是(可能是舊的)的Java方法。 C#有屬性。 – krillgar

+0

看到這個答案:https://stackoverflow.com/questions/1568091/why-use-getters-and-setters –

回答

4

「最佳」的方式是使用C#語言功能,即性能

class Dog 
{ 
    public string Name {get;} // read-only property (can be set in constructor) 
    public string Breed {get;} 

    public Dog(string name, string breed) 
    { 
     this.Name = name; 
     this.Breed = breed; 
    } 
} 
+0

不知道爲什麼這是downvoted,但{{;}的語法是正確的後C#4.6(maaaaybe 4.5.1)。這使它成爲在構造函數中設置的只讀屬性。 – krillgar

+0

@krillgar沒有C#4.6。這是C#6(它可以針對不同的.NET框架版本,如.NET 4.6)的有效語法。無可否認,這些版本號會令人困惑,因爲新的框架版本通常會與新的編譯器同時發佈。 – Kyle

+0

@凱爾是的,我的壞。我正在做一些事情和錯過的事情。感謝您的更正。 – krillgar

2

在C#中,我們有一個語言結構稱爲properties,我認爲他們是最合適的這裏。通常,屬性用於公開對象狀態,優先於方法,並且可以使其成爲只讀或讀寫。

class Dog 
{ 
    public string Name { get; private set; } 
    public string Breed { get; private set; } 

    public Dog(string name, string breed) 
    { 
     Name = name; 
     Breed = breed; 
    } 
} 

C#中的屬性有很多味道,所以我建議在文檔中閱讀它們。

1

通常,方法表示動作和屬性表示數據。 在這種情況下,我將使用Property,因爲屬性的值存儲在進程內存中,並且屬性只是提供對該值的訪問。

如果屬性的值存儲在進程內存和 屬性將只提供訪問值使用屬性,而不是方法,

使用的方法,而不是一個性質,在下列情況下。

  • 該操作的速度比字段集 的要慢幾個數量級。如果您甚至考慮提供異步版本的 避免阻塞線程的操作,那麼很可能 操作對於屬性來說太昂貴了。特別是, 訪問網絡或文件系統的操作( 一次用於初始化)應該很可能是方法,而不是 屬性。
  • 該操作是一種轉換,例如Object.ToString方法。
  • 每次調用該操作時都會返回不同的結果,如果參數不變,即使爲 。例如,NewGuid方法 每次調用時都會返回一個不同的值。
  • 該操作具有顯着的可觀察的副作用。請注意,填充內部緩存的 通常不被視爲可觀察到的副作用。
  • 該操作返回內部狀態的副本(這不包括 包括在堆棧上返回的值類型對象的副本)。 操作返回一個數組。

閱讀這篇文章,以獲取更多的代碼示例信息。

Choosing Between Properties and Methods

+0

我認爲你鏈接的文章真的不錯 –

+0

是的。這是一篇非常好的文章,用實例解釋了原因。 – Sampath

+0

事實上,我upvoted你的答案 –

0

您可以使用屬性來封裝領域。

class Program 
    { 
     static void Main(string[] args) 
     { 
      Dog fuffy = new Dog("Fuffy", "Amarant"); 
      Console.WriteLine(fuffy.Name); 
      Console.WriteLine(fuffy.Breed); 
    } 
} 

class Dog 
{ 
    private string name; 
    public string Name 
    { 
     get { return name; } 
     set { name = value; } 
    } 
    private string breed; 
    public string Breed 
    { 
     get { return breed; } 
     set { breed = value; } 
    } 
    public Dog(string pName, string pBreed) 
    { 
     Breed = pBreed; 
     Name = pName; 
    } 
} 
0

我要說的是,他們既不是「最好的」,但我會用C#的內置properties

public string Breed {get;set;} 

如果你想防止被設置的品種,你可以刪除set

public string Breed {get;} 

在你的例子中,getBreed是隻讀的,相當於我的第二個例子。

修改類,看起來像這樣:

class Program 
{ 
    static void Main(string[] args) 
    { 
     Dog fuffy = new Dog("Fuffy", "Armant"); 
     Console.WriteLine(fuffy.breed); //Prints Fuffy 
     Console.WriteLine(fuffy.breed = "Muffy"); //Prints Muffy 
     Console.WriteLine(fuffy.breed_readonly = "Muffy"); //World ends in exception 
    } 
} 


class Dog 
{ 
    public string name {get;set;} 
    public string breed {get;set;} 

    public string name_readonly {get;} 
    public string breed_readonly {get;} 

    public Dog(string name, string breed) 
    { 
     this.name = name; 
     this.breed = breed; 

     this.name_readonly = name; 
     this.breed_readonly = breed; 
    } 
} 
相關問題