같은 클래스의 함수만 private 멤버에 액세스할 수 있습니다. 개인 액세스 지정자는 클래스가 다른 함수 및 개체로부터 자신의 구성원 변수 및 구성원 함수를 숨길 수 있도록 합니다.
예시
using System;
namespace RectangleApplication {
class Rectangle {
//member variables
private double length;
private double width;
public void Acceptdetails() {
length = 10;
width = 14;
}
public double GetArea() {
return length * width;
}
public void Display() {
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
} //end class Rectangle
class ExecuteRectangle {
static void Main(string[] args) {
Rectangle r = new Rectangle();
r.Acceptdetails();
r.Display();
Console.ReadLine();
}
}
} 출력
Length: 10 Width: 14 Area: 140
위에서 가변 길이와 너비는 private로 선언되었습니다. 따라서 동일한 클래스의 메소드가 액세스할 수 있습니다.