訪問修飾符的用途是隱藏數據成員以防未經授權的訪問。而屬性的目的是揭示Access修飾符。訪問修飾符的用途死亡。以下是例子。訪問修飾符VS屬性
public class Employee
{
private int EmployeeID;
private string Name;
private int Salary;
public int EID { get { return this.EmployeeID; } set { this.EmployeeID = value; } }
public string EName { get { return this.Name; } set { this.Name = value; } }
public int ESalary { get { return this.Salary; } set { this.Salary = value; } }
}
static void Main(string[] args)
{
Employee Employee = new Employee();
Employee.EName = "Zaheer";
}
在這裏我可以訪問屬性名稱,它間接訪問名稱。如果問題很愚蠢,任何評論都會很抱歉。
是的,你是對的,但事情是,我仍然可以訪問私人會員。我認爲它不應該被允許.... – 2013-03-20 14:30:41
也看你的語法,你會想要標準化你的領域的命名是這樣的:公共領域(也稱爲屬性)將有他們的名字以大寫字母開頭,而私人領域將以小寫字母開頭。 – 2013-03-20 14:34:07
您不能訪問其他所屬類別的私人成員。您在代碼中訪問的內容是公開的EName屬性。 – 2013-03-20 14:35:46