1

私は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() を非表示にする良い方法ですか?.

4

2 に答える 2

3

OO の観点から見ると、ShowDetails メソッドは 2 つのまったく異なることを行っています。

  • オブジェクトを表す文字列の作成
  • 文字列を HttpResponse に出力します。

ここで、最初のタスクはクラス Employee に属しています。オブジェクトを表す文字列を作成できる従業員を知る必要があります。実際、.net では、これは非常に一般的なことです。実際には、Object.ToString() と呼ばれる「オーバーライド可能な」または「仮想」関数があります。

2 番目のタスクはクラス Employee とはまったく関係がなく、文字列と HttpResponses とは多くの関係があります (この場合、HttpResponse を取得する方法、つまり HttpContext から取得する方法、つまり Web サーバー上にいる必要があります) HttpRequest で)。これらすべての仮定により、万能の「データ」または「ドメイン」クラスでは非常に安全ではありません。

これが私がこれをリファクタリングする方法です。

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

Web ページ内に有効な HttpContext.Current があることが確実にわかっているためです。したがって、従業員はインターネットについて知る必要がなく、WinForm アプリケーションで同様に作業できます。

于 2013-02-06T07:25:51.110 に答える
1

あなたの主な疑問は良いものだと思います。あなたの従業員オブジェクトは、HttpContext への書き込みなど、おそらく所有すべきではない責任を負っています。この出力文字列が一般的である場合は、.NET にある ToString 演算子をオーバーライドし、ShowDetails を削除して、ボタンのクリック時にこれを追加することができます。

HttpContext.Current.Response.Write(emp.ToString())
于 2013-02-06T06:52:07.500 に答える