2013-02-06 158 views
1

我正在研究OOP概念。正如我從我讀過的文檔中所理解的那樣,我爲OOP中的封裝概念編寫了一個示例程序。我在下面粘貼了我的代碼。我的封裝概念是正確的嗎?封裝在OOP中

Default.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(); 
} 
} 

我的主要懷疑是關於我在Employee中調用ShowDetails()方法的方式。這是隱藏ShowDetails()方法的好方法嗎?

+0

封裝不是關於隱藏方法,請考慮對此問題的解答:http://stackoverflow.com/questions/18300953/why-encapsulation-is-important-feature-of-oop-languages/ – BartoszKP

回答

3

從面向對象的角度來看,你ShowDetails方法是做兩個非常不同的事情。

  • 創建表示對象
  • 輸出字符串到的HttpResponse的字符串。

現在第一個任務屬於Employee類,您需要知道員工能夠創建一個代表該對象的字符串。 事實上,在.net中這是一個很常見的事情,實際上有一個名爲Object.ToString()的「overridable」或「virtual」函數。

第二個任務具有絕對無關Employee類的,很多做字符串和HttpResponses(在這種情況下我們如何獲取HttpResponse,這是從的HttpContext得到它,這意味着我們必須在HttpRequest中的Web服務器上)。有了所有這些假設,對於全部目的「數據」或「域」類來說,這是非常不安全的。

這就是我要如何重構這個。

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 
    } 

    public override string ToString() 
    { 
     return this.GetEmployeeID() + " : " + 
      this.EmployeeName + " : " + 
      this.EmployeeAge + " : " + 
      this.GetEmployeeSalary(); 
    } 


} 

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) 
    { 
     HttpContext.Current.Response.Write(emp.ToString()); 
    } 
} 

因爲我們知道肯定有網頁內有效HttpContext.Current。因此,員工不需要了解互聯網,也可以在WinForm應用程序上工作。

1

我認爲你的主要疑問是一個好的。您的員工對象正在承擔它可能不應擁有的責任,如寫入HttpContext。如果輸出字符串是常見的,你可能會覆蓋在.NET中發現的ToString操作,刪除ShowDetails,並添加這對您的按鈕點擊:

HttpContext.Current.Response.Write(emp.ToString())