在實際環境中測試
在生產環境中測試無浮水印。
在任何需要的地方都能運作。
取值器和設值器是物件導向程式設計語言(如 C#)中的基本概念。這兩個方法允許我們控制類別屬性的訪問和修改。在本教程中,我們將通過代碼塊探索 C# 取值器和私有設值器的初學者和中級方面。
從本質上講,getter 和 setter 不過是類中具有相同名稱的方法。Getter 會返回私有字段變量的值,而 setter 則會修改其重要性。這些方法確保了一個類的內部數據 (欄位) 是以安全和正確的方式訪問或修改。
假設我們有一個 public class car
,其中有一個 private string description property name
。如果這個類別之外的某個人想要知道描述,他們不能直接訪問它,因為它是私有的。在這裡,getter 和 setter 發揮了作用。
getter 允許使用者在不直接修改 private string description
值的情況下檢索它的值。另一方面,setter 讓使用者可以在我們設定的條件下修改隱式參數 description。
訪問修飾符定義了從類外訪問字段或屬性的可見性和可訪問性的級別。最常用的修飾符是 public
和 private
。
Public
: 宣告為 public
的字段或屬性可以在程式的任何地方進行訪問。例如,你可能有一個 public int age
屬性,可以由程式碼的任何部分訪問和修改。Private
: 宣告為 private
的屬性或字段只能在定義的類內部進行訪問,例如 private int age
。這種封裝有助於防止未授權的修改,確保類的內部狀態得到妥善維護。讓我們從一個簡單的例子開始。
//set accessor
public class Person
{
private string name; // This is a private string name property variable
public string GetName() // This is the getter
{
return name;
}
public void SetName(string newName) // This is the setter, new value
{
name = newName;
}
}
static void Main()
{
Person person = new Person();
person.SetName("John");
Console.WriteLine(person.GetName());
}
//set accessor
public class Person
{
private string name; // This is a private string name property variable
public string GetName() // This is the getter
{
return name;
}
public void SetName(string newName) // This is the setter, new value
{
name = newName;
}
}
static void Main()
{
Person person = new Person();
person.SetName("John");
Console.WriteLine(person.GetName());
}
'set accessor
Public Class Person
Private name As String ' This is a private string name property variable
Public Function GetName() As String ' This is the getter
Return name
End Function
Public Sub SetName(ByVal newName As String) ' This is the setter, new value
name = newName
End Sub
End Class
Shared Sub Main()
Dim person As New Person()
person.SetName("John")
Console.WriteLine(person.GetName())
End Sub
在上述的 class Person
中,我們有一個 getter (取得名稱) 和設置器 (設置名稱) 對於 name
屬性。當你執行 static void Main
方法時,它將列印 "John",因為我們使用 setter 設定了該值。
現在,你可能會想,「我是否總是需要為 getter 和 setter 編寫單獨的方法?」答案是「不需要。」C# 引入了一個名為「自動實現屬性」或「自動屬性」的概念,以簡化這個過程。
在 C# 中,你可以使用自動屬性,這為你提供了一種宣告 private field
及其相關屬性的簡便方法。下面是一個例子:
public class Student
{
public string Name { get; set; } // This is an auto-implemented or automatic property, public string Name
public string Title {get; set;}
}
static void Main()
{
Student student = new Student();
student.Name = "Alice"; // Using setter
Console.WriteLine(student.Name); // Using getter
}
public class Student
{
public string Name { get; set; } // This is an auto-implemented or automatic property, public string Name
public string Title {get; set;}
}
static void Main()
{
Student student = new Student();
student.Name = "Alice"; // Using setter
Console.WriteLine(student.Name); // Using getter
}
Public Class Student
Public Property Name() As String ' - This is an auto-implemented or automatic property, public string Name
Public Property Title() As String
End Class
Shared Sub Main()
Dim student As New Student()
student.Name = "Alice" ' Using setter
Console.WriteLine(student.Name) ' Using getter
End Sub
在 Student
class
中,Name
屬性同時具有 getter 和 setter,並且是自動實現的。C# 編譯器在後台創建了一個 private string name
字段,Name
屬性提供對該字段的訪問。
有時候,你可能會希望提供一個可以讀取但不能外部修改的屬性。這就是只讀屬性的用武之地。你可以省略屬性中的設定器來使其成為只讀。
假設我們想在我們的 Person
類別中添加一個只讀的 string Description
屬性:
public class Person
{
public string Name { get; set; }
public string Description { get; }
public Person(string name, string description)
{
Name = name;
Description = description;
}
}
public class Person
{
public string Name { get; set; }
public string Description { get; }
public Person(string name, string description)
{
Name = name;
Description = description;
}
}
Public Class Person
Public Property Name() As String
Public ReadOnly Property Description() As String
Public Sub New(ByVal name As String, ByVal description As String)
Me.Name = name
Me.Description = description
End Sub
End Class
在此範例中,Description
屬性只能在 Person
類別的建構子內設定,用更少的程式碼。一旦設定後,就無法在外部修改。
有時候,你可能希望允許屬性可以從類別外部讀取,但只能在類別內部設置。這可以使用 private set
來實現。
public class Program
{
public string Description { get; private set; }
public Program()
{
Description = "This is a program about getters and setters.";
}
}
class ProgramTest
{
static void Main()
{
Program myProgram = new Program();
Console.WriteLine(myProgram.Description); // Allowed
// myProgram.Description = "New Description"; // Not allowed
}
}
public class Program
{
public string Description { get; private set; }
public Program()
{
Description = "This is a program about getters and setters.";
}
}
class ProgramTest
{
static void Main()
{
Program myProgram = new Program();
Console.WriteLine(myProgram.Description); // Allowed
// myProgram.Description = "New Description"; // Not allowed
}
}
Public Class Program
Private privateDescription As String
Public Property Description() As String
Get
Return privateDescription
End Get
Private Set(ByVal value As String)
privateDescription = value
End Set
End Property
Public Sub New()
Description = "This is a program about getters and setters."
End Sub
End Class
Friend Class ProgramTest
Shared Sub Main()
Dim myProgram As New Program()
Console.WriteLine(myProgram.Description) ' Allowed
' myProgram.Description = "New Description"; // Not allowed
End Sub
End Class
在 class Program
中,Description
屬性有一個 private set
,這意味着它不能從類外部進行更改,以確保數據的完整性。
在處理派生類別時,您可以覆寫getter和setter方法來自訂它們的行為。這使得您可以在獲取或設定值時添加額外的邏輯。
public class Person
{
public virtual string Name { get; set; }
}
public class Student : Person
{
private string studentID;
public override string Name
{
get { return base.Name; }
set
{
if (!string.IsNullOrEmpty(value))
base.Name = value;
}
}
}
public class Person
{
public virtual string Name { get; set; }
}
public class Student : Person
{
private string studentID;
public override string Name
{
get { return base.Name; }
set
{
if (!string.IsNullOrEmpty(value))
base.Name = value;
}
}
}
Public Class Person
Public Overridable Property Name() As String
End Class
Public Class Student
Inherits Person
Private studentID As String
Public Overrides Property Name() As String
Get
Return MyBase.Name
End Get
Set(ByVal value As String)
If Not String.IsNullOrEmpty(value) Then
MyBase.Name = value
End If
End Set
End Property
End Class
在此範例中,Student
類別繼承自 Person
,並且覆寫了 Name
屬性的方法。它在設定名稱之前新增了一個驗證檢查,確保名稱不為空或 null。
Iron Suite 是一組增強 C# 開發能力的研究工具。它包括 IronPDF、IronXL、IronOCR 和 IronBarcode。這些工具每一個都有其獨特的用途,可以整合到 C# 的各個方面。
IronPDF
是一個函式庫,允許開發人員在 C# 中創建、閱讀和編輯 PDF 文件。不論是使用 HTML 轉 PDF 教程
或者通過getters和setters管理PDF元數據,IronPDF可以滿足你的需求。
當處理Excel文件時, IronXL
簡化讀寫過程。此工具可用於操作 Excel 文件中的私人和公共字串或整數,類似於在 C# 中使用相同的語法來處理類中的資料時使用 getter 和 setter。
IronOCR
是一個光學字符識別(OCR)函式庫,能夠將圖像轉換為可搜索的文本。如果您的應用程式涉及從掃描的文件中讀取文本,那麼 IronOCR 的強大功能可以輕鬆整合進去。它可以處理私有字段和公共字符串描述,就像您在之前的示例設置中的類 Person 或類 Student 中所期望的一樣。
IronBarcode是一款用於條碼讀取和寫入的基本工具。它允許直接訪問條碼數據並通過自動屬性進行自定義,就像C#編程中使用的getter和setter一樣。
Iron Suite 套件與C#開發無縫整合,包括getters和setters。這些工具為任何C#項目增值。
總結:
Getter 和 Setter
: 它們幫助訪問和修改類的私有字段。
Auto Properties
: 通過自動實現的屬性,一種優雅的方式來實現自動的 getter 和 setter。
Access Modifiers
: 它們幫助細化屬性的可訪問性。現在,你應該對如何在 C# 中使用 getter 和 setter 有了堅實的理解。
Iron Suite 的工具套件為 C# 開發人員提供了令人難以置信的功能。這些產品如 IronPDF, IronXL, IronOCR 和 IronBarcode,都具備 免費試用,允許您在沒有任何初期投資的情況下探索並整合這些強大的庫到您的項目中。
當您準備好投入時,個別授權從$749起。如果您發現不只一個這些工具符合您的需求,您可以利用機會以兩個個別授權的價格購買完整的Iron Suite套餐。