1.22. C # package

发布时间 : 2025-10-25 13:34:34 UTC      

Page Views: 9 views

Encapsulation is defined as “enclosing one or more projects in a physical or logical package”. In object-oriented programming methodology, encapsulation is designed to prevent access to implementation details.

Abstraction and encapsulation are related features of object-oriented programming. Abstraction allows visualization of related information, while encapsulation enables developers to achieve the required level of abstraction.

According to the specific needs, C# encapsulation sets the access rights of users, and realizes it through access modifiers.

An access modifier defines the scope and visibility of a class member. The access modifiers supported by C# are as follows:

  • Public: all objects are accessible;

  • Private: the object itself can be accessed inside the object;

  • Protected: only this class object and its subclass objects can access

  • Internal: Objects in the same assembly can be accessed;

  • Protected internal: access is limited to the current assembly or a type derived from the containing class.

Image0

1.22.1. Public access modifier #

Public access modifiers allow a class to expose its member variables andmember functions to other functions and objects. Any public member can beaccessed by an external class.

The following example illustrates this:

Example #

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()); } }// Rectangle end 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 results:

Length: 4.5 Width: 3.5 Area: 15.75 

In the above example, the member variable length and width are declared as public , so they cannot be used by functions Main() accessing instances of the Rectangle class using r .

Member function Display() and GetArea() , you can access these variables directly.

Member function Display() is also declared to be public , so it canalso be accessed by r instances of the Rectangle class using Main() .

1.22.2. Private access modifier #

Private access modifiers allow a class to hide its member variables and member functions from other functions and objects. Only functions in the same class can access its private members. Even an instance of a class cannot access its private members.

The following example illustrates this:

Example #

using System; namespace RectangleApplication { class Rectangle { //Member variables private double length; private double width; public void Acceptdetails() { Console.WriteLine("Please enter the length:"); length = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Please enter the 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 results:

Please enter the length: four point four Please enter the width: three point three Length: 4.4 Width: 3.3 Area: 14.52 

In the above example, the member variable length and width are declared as private , so they cannot be used by functions Main() visit.

Member function AcceptDetails() and Display() , which you can access thesevariables.

Because of the member function AcceptDetails() and Display() are declared as public , so they can be accessed by r instances of the Rectangle class using Main() .

1.22.3. Protected access modifier #

The Protected access modifier allows a subclass to access member variables and member functions of its base class. This helps to implement inheritance.We will discuss this in detail in the chapter on inheritance. Discuss this in more detail.

1.22.4. Internal access modifier #

Internal access specifiers allow a class to expose its member variables and member functions to other functions and objects in the current program. In other words, with internal any member of the access modifier can be accessed by any class or method within the application defined by that member.

The following example illustrates this:

Example #

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 results:

Length: 4.5 Width: 3.5 Area: 15.75 

In the example above, notice the member function GetArea() declared without any access modifiers. If no access modifier is specified, the default access modifier for class members is used, that is, private .

1.22.5. Protected Internal access modifier #

Protected Internal access modifiers are allowed to be accessed in this class, derived classes, or assemblies that contain the class. This is also used to implement inheritance.

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。