私はOOPの概念について勉強しています。私が読んだドキュメントから理解したように、OOP でのカプセル化の概念のサンプル プログラムを作成しました。以下にコードを貼り付けました。カプセル化に関する私の概念は正しいですか?
デフォルト.aspx
<asp:Button ID="showBtn" Text="Show employee details." runat="server"/>
Default.aspx.cs
public partial class _Default : System.Web.UI.Page
{
Employee emp;
protected void Page_Load(object sender, EventArgs e)
{
emp = new Employee();
emp.SetEmployeeID(001);
emp.SetEmployeeSalary(5000);
emp.EmployeeName = "Rob";
emp.EmployeeAge = 26;
showBtn.Click += new EventHandler(showBtn_Click);
}
void showBtn_Click(object sender, EventArgs e)
{
emp.ShowEmployeeDetails();
}
}
クラス社員
class Employee
{
private int empId;
private int empSalary;
private string empName;
private int empAge;
public void SetEmployeeID(int id)
{
empId = id; //Mutator
}
public void SetEmployeeSalary(int sal)
{
empSalary = sal; //Mutator
}
public int GetEmployeeID()
{
return empId; //Accessor
}
public int GetEmployeeSalary()
{
return empSalary; //Accessor
}
public string EmployeeName
{
get { return empName; } //Accessor
set { empName = value; } //Mutator
}
public int EmployeeAge
{
get { return empAge; } //Accessor
set { empAge = value; } //Mutator
}
private void ShowDetails()
{
HttpContext.Current.Response.Write(this.GetEmployeeID() + " : " + this.EmployeeName + " : " + this.EmployeeAge + " : " + this.GetEmployeeSalary());
}
public void ShowEmployeeDetails()
{
ShowDetails();
}
}
私の主な疑問は、従業員で ShowDetails() メソッドを呼び出した方法についてです。これはメソッド ShowDetails() を非表示にする良い方法ですか?.