2013-02-06 4 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()이라는 "오버라이드 가능"또는 "가상"함수가 있습니다.

두 번째 작업은 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 응용 프로그램에서 작업 할 수 있습니다.

1

나는 당신의 주된 의심이 좋은 것이라고 생각한다. 직원 객체는 HttpContext에 쓰는 것처럼 소유해서는 안되는 책임을지고 있습니다. 이 출력 문자열이 일반적인 경우, .NET에서 발견의 toString 연산자를 대체 ShowDetails를 제거하고 버튼 클릭에이를 추가 할 수 있습니다 : 객체 지향 관점에서

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