我正在研究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()的好方法吗?

最佳答案

从面向对象的角度来看,您的ShowDetails方法正在做两个非常不同的事情。


创建一个表示对象的字符串
将字符串输出到HttpResponse。


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

第二项任务与Employee类完全无关,而与字符串和HttpResponses无关(在这种情况下,我们如何获取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应用程序。

09-26 17:46