내부 액세스 지정자
내부 액세스 지정자는 클래스가 해당 멤버 변수와 멤버 함수를 현재 어셈블리의 다른 함수 및 개체에 노출할 수 있도록 합니다.
내부 액세스 지정자가 있는 모든 멤버는 해당 멤버가 정의된 응용 프로그램 내에 정의된 모든 클래스 또는 메서드에서 액세스할 수 있습니다.
다음은 예입니다 -
예
using System;
namespace RectangleApplication {
class Rectangle {
//member variables
internal double length;
internal double width;
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.length = 4.5;
r.width = 3.5;
r.Display();
Console.ReadLine();
}
}
} 비공개 액세스 지정자
개인 액세스 지정자를 사용하면 클래스가 다른 함수 및 개체에서 해당 구성원 변수 및 구성원 함수를 숨길 수 있습니다. 같은 클래스의 함수만 private 멤버에 액세스할 수 있습니다.
다음은 예입니다 -
예
using System;
namespace RectangleApplication {
class Rectangle {
private double length;
private double width;
public void Acceptdetails() {
length = 10;
width = 15;
}
public double GetArea() {
return length * width;
}
public void Display() {
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
class ExecuteRectangle {
static void Main(string[] args) {
Rectangle r = new Rectangle();
r.Acceptdetails();
r.Display();
Console.ReadLine();
}
}
}