C # object-oriented three characteristics: a package

What is the package

Definition: the one or more items enclosed in a physical or logical packets. In object-oriented design methodology, the package is to prevent access to implementation details.

The advantage of the package

1. isolation, security. After being packaged objects (object code refers to here is a programming unit, generally refers to: assembly, namespaces, classes, methods, attributes, variables, etc.) inside which the external object is not directly access the object's implementation details, the internal implementation details of the changes will not affect the principle of access to external objects.

2. readability. The name of the encapsulated object properly, you can not see the case of the specific implementation, to understand the role of the object

3. good encapsulation can reduce the coupling (such as the interface and achieve the logical separation)

4. Class has a clear external interface, users simply call, do not care about the internal

5. Functional package as independent, it is possible to achieve better code reuse

Access modifier

C # package according to specific needs, the user is provided access privileges, and is achieved by access modifiers.

An access modifier defines the scope and visibility of a class member. C # supports access modifier as follows:

  • public: all objects can be accessed;
  • private: the object itself can be accessed inside the object;
  • protected: Only class object can access the object and its subclasses
  • internal: a set of objects of the same program can be accessed;
  • protected internal: restricting access to or derived from the current assembly of the type comprising a class.

Public access modifier

Public access modifier allows a class to expose its member variables and member functions to other functions and objects. Any member of the public can be accessed outside the class.

The following example illustrates this point:

using System;

namespace RectangleApplication
{
    class Rectangle
    {
        //成员变量
        public double length;
        public double width;

        public double GetArea()
        {
            return length * width;
        }
        public void Display()
        {
            Console.WriteLine("长度: {0}", length);
            Console.WriteLine("宽度: {0}", width);
            Console.WriteLine("面积: {0}", GetArea());
        }
    }// 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:

In the example above, the member variable length and width are declared as public, they may be a function of Main () Example Rectangle r access class.

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

成员函数 Display() 也被声明为 public,所以它也能被 Main() 使用 Rectangle 类的实例 r 访问。

Private 访问修饰符

Private 访问修饰符允许一个类将其成员变量和成员函数对其他的函数和对象进行隐藏。只有同一个类中的函数可以访问它的私有成员。即使是类的实例也不能访问它的私有成员。

下面的实例说明了这点:

using System;

namespace RectangleApplication
{
    class Rectangle
    {
        //成员变量
        private double length;
        private double width;

        public void Acceptdetails()
        {
            Console.WriteLine("请输入长度:");
            length = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("请输入宽度:");
            width = Convert.ToDouble(Console.ReadLine());
        }
        public double GetArea()
        {
            return length * width;
        }
        public void Display()
        {
            Console.WriteLine("长度: {0}", length);
            Console.WriteLine("宽度: {0}", width);
            Console.WriteLine("面积: {0}", GetArea());
        }
    }//end class Rectangle
    
    class ExecuteRectangle
    {
        static void Main(string[] args)
        {
            Rectangle r = new Rectangle();
            r.Acceptdetails();
            r.Display();
            Console.ReadLine();
        }
    }
}

当上面的代码被编译和执行时,它会产生下列结果:

尝试访问私有成员length会出现报错信息:

在上面的实例中,成员变量 length 和 width 被声明为 private,所以它们不能被函数 Main() 访问。

成员函数 AcceptDetails() 和 Display() 可以访问这些变量。

由于成员函数 AcceptDetails() 和 Display() 被声明为 public,所以它们可以被 Main() 使用 Rectangle 类的实例 r 访问。

Protected 访问修饰符

Protected 访问修饰符允许子类访问它的基类的成员变量和成员函数。这样有助于实现继承。我们将在继承的章节详细讨论这个。更详细地讨论这个。

Internal 访问修饰符

Internal 访问说明符允许一个类将其成员变量和成员函数暴露给当前程序中的其他函数和对象。换句话说,带有 internal 访问修饰符的任何成员可以被定义在该成员所定义的应用程序内的任何类或方法访问。

下面的实例说明了这点:

using System;

namespace RectangleApplication
{
    class Rectangle
    {
        //成员变量
        internal double length;
        internal double width;
       
        double GetArea()
        {
            return length * width;
        }
       public void Display()
        {
            Console.WriteLine("长度: {0}", length);
            Console.WriteLine("宽度: {0}", width);
            Console.WriteLine("面积: {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();
        }
    }
}

当上面的代码被编译和执行时,它会产生下列结果:

 在另一应用程序中调用Rectangle以及Rectangle的内部成员都会提示错误信息

namespace Chapter04
{
  class ExecuteRectangle
     {
        static void Main(string[] args)
        {
            Rectangle r = new Rectangle();
            r.length = 4.5;
            r.width = 3.5;
            r.Display();
            Console.ReadLine();
        }
    }
}

在上面的实例中,请注意类Rectangle声明的时候不带有任何访问修饰符,默认为Internal;成员函数 GetArea() 声明的时候不带有任何访问修饰符。如果没有指定访问修饰符,则使用类成员的默认访问修饰符,即为 private

Protected Internal 访问修饰符

Protected Internal 访问修饰符允许在本类,派生类或者包含该类的程序集中访问。这也被用于实现继承。

默认访问修饰符

在命名空间内部或编译单元顶部的所有类型(class、struct、abstract class、interface、delegate、enum),默认是internal,可以人为改为public

类中所有的成员,默认均为private

抽象类的所有成员,默认均为private类型,但抽象方法不能用private修饰

接口的所有成员,默认均为public类型,而且不能手动添加访问修饰符

结构的所有成员,默认均为private类型,而且只能是public、internal、private这三种类型

命名空间,枚举类型成员默认public,也不可能是其他访问修饰符

委托,默认internal

 

Guess you like

Origin www.cnblogs.com/zhaoyl9/p/11597632.html