Csharp 简明教程
C
Encapsulation 定义为“将一个或多个项目包含在一个物理或逻辑包之中”。封装在面向对象编程方法中,阻止访问实现细节。
Encapsulation is defined 'as the process of enclosing one or more items within a physical or logical package'. Encapsulation, in object oriented programming methodology, prevents access to implementation details.
抽象和封装是面向对象编程中的相关特征。抽象使相关信息可见,而封装能够使程序员实现所需的抽象级别。
Abstraction and encapsulation are related features in object oriented programming. Abstraction allows making relevant information visible and encapsulation enables a programmer to implement the desired level of abstraction.
封装是通过使用 access specifiers 实现的。一个 access specifier 定义成员的范围和可视性。C# 支持以下访问说明符:
Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member. C# supports the following access specifiers −
-
Public
-
Private
-
Protected
-
Internal
-
Protected internal
Public Access Specifier
public 访问说明符允许一个类向其他函数和对象公开其成员变量和成员函数。任何 public 成员都可以从类外部访问。
Public access specifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed from outside the class.
以下示例说明此情况:
The following example illustrates this −
using System;
namespace RectangleApplication {
class Rectangle {
//member variables
public double length;
public double width;
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.length = 4.5;
r.width = 3.5;
r.Display();
Console.ReadLine();
}
}
}
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
Length: 4.5
Width: 3.5
Area: 15.75
在前一个示例中,成员变量 length 和 width 声明为 public ,因此它们可以使用名为 r 的矩形类的实例从函数 Main() 中进行访问。
In the preceding example, the member variables length and width are declared public, so they can be accessed from the function Main() using an instance of the Rectangle class, named r.
成员函数 Display() 和 GetArea() 也能直接访问这些变量,无需使用该类的任何实例。
The member function Display() and GetArea() can also access these variables directly without using any instance of the class.
成员函数 Display() 也声明为 public ,因此它也能使用名为 r 的矩形类的实例从 Main() 中进行访问。
The member functions Display() is also declared public, so it can also be accessed from Main() using an instance of the Rectangle class, named r.
Private Access Specifier
Private 访问说明符允许某个类将其的成员变量和成员函数从其他函数和对象中隐藏起来。只有该类的函数才能访问其私有成员。即使一个类的实例也无法访问其私有成员。
Private access specifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members. Even an instance of a class cannot access its private members.
以下示例说明此情况:
The following example illustrates this −
using System;
namespace RectangleApplication {
class Rectangle {
//member variables
private double length;
private double width;
public void Acceptdetails() {
Console.WriteLine("Enter Length: ");
length = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter Width: ");
width = Convert.ToDouble(Console.ReadLine());
}
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();
}
}
}
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
Enter Length:
4.4
Enter Width:
3.3
Length: 4.4
Width: 3.3
Area: 14.52
在前一个示例中,成员变量 length 和 width 被声明为 private ,因此它们无法从函数 Main() 中进行访问。成员函数 AcceptDetails() 和 Display() 能够访问这些变量。由于成员函数 AcceptDetails() 和 Display() 被声明为 public ,因此它们可以使用名为 r 的矩形类的实例从 Main() 中进行访问。
In the preceding example, the member variables length and width are declared private, so they cannot be accessed from the function Main(). The member functions AcceptDetails() and Display() can access these variables. Since the member functions AcceptDetails() and Display() are declared public, they can be accessed from Main() using an instance of the Rectangle class, named r.
Protected Access Specifier
Protected 访问说明符允许子类访问其基类的成员变量和成员函数。以这种方式帮助实现继承。我们将在继承章节中更详细地讨论这一点。
Protected access specifier allows a child class to access the member variables and member functions of its base class. This way it helps in implementing inheritance. We will discuss this in more details in the inheritance chapter.
Internal Access Specifier
Internal 访问说明符允许某个类将其的成员变量和成员函数公开给当前程序集中的其他函数和对象。换句话说,可以使用定义了成员的应用程序中定义的任何类或方法访问具有内部访问说明符的任何成员。
Internal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current assembly. In other words, any member with internal access specifier can be accessed from any class or method defined within the application in which the member is defined.
以下程序说明了这一点 −
The following program illustrates this −
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();
}
}
}
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
Length: 4.5
Width: 3.5
Area: 15.75
在前一个示例中,请注意成员函数 GetArea() 没有用任何访问说明符进行声明。那么,如果我们未提及任何访问说明符,类的成员的默认访问说明符是什么?它是 private 。
In the preceding example, notice that the member function GetArea() is not declared with any access specifier. Then what would be the default access specifier of a class member if we don’t mention any? It is private.
Protected Internal Access Specifier
Protected internal 访问说明符允许某个类将其的成员变量和成员函数从其他类对象和函数中隐藏起来,但同个应用程序中的子类除外。这在实施继承时也会使用。
The protected internal access specifier allows a class to hide its member variables and member functions from other class objects and functions, except a child class within the same application. This is also used while implementing inheritance.