1

我正在研究 OOP 概念。从我阅读的文档中了解到,我为 OOP 中的封装概念编写了一个示例程序。我在下面粘贴了我的代码。我关于封装的概念是否正确?

默认.aspx

<asp:Button ID="showBtn" Text="Show employee details." runat="server"/>

默认.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() 的好方法吗?

4

2 回答 2

3

从 OO 的角度来看,您的 ShowDetails 方法正在做两件非常不同的事情。

  • 创建代表对象的字符串
  • 将字符串输出到 HttpResponse。

现在第一个任务确实属于 Employee 类,您需要知道员工是什么才能创建代表对象的字符串。事实上,在 .net 中这是很常见的事情,实际上有一个名为 Object.ToString() 的“可覆盖”或“虚拟”函数。

第二个任务与 Employee 类完全无关,与字符串和 HttpResponse 有很多关系(在这种情况下,我们如何获取 HttpResponse,即从 HttpContext 获取它,这意味着我们必须在网络服务器上在 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());
    }
}

因为我们确定网页中有一个有效的 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 回答