跳转到内容

C Sharp/Encapsulation

维基教科书,自由的教学读本

封装(Encapsulation)是把类的方法、属性、数据成员不让用户访问或者限制其访问。C#的类的成员默认是private访问。

public class Frog
{
    private int _height = 0;

    // Methods
    public void JumpLow() { Jump(1); }
    public void JumpHigh() { Jump(10); }

    void Jump(int height) { _height += height; }
}


保护级别

[编辑]

Private

[编辑]

Private成员只有类内的代码可以访问。这是缺省的保护级别。

namespace PrivateSample
{
    public class Person
    {
        private string _name;

        // Methods
        public Person(string name)
        {
            // Private members can only be modified by the internal methods or constructors of class
            this._name = name; 
        }
    }

    public class Entry
    {
        static void Main(string[] args)
        {
            Person OnePerson = new Person("Samanta");
            //OnePerson._name = "Sam"; // This causes a error of access level
        }
    }
}

Protected

[编辑]

Protected的成员可以由派生类的代码访问。

namespace ProtectedSample
{
    public class Person
    {
        protected string _name;
    }
    /// <summary>
    /// When a class inherits from other class, it can access your protected and public members
    /// above your created members
    /// </summary>
    public class Warrior : Person
    {
        public void SetName(string name)
        {
            // Protected members can be accessed by internal methods or constructors of class
            // so, it can be accessed by inherit class too
            base._name = name;
        }
    }

    public class Entry
    {
        static void Main(string[] args)
        {
            Warrior OnePerson = new Warrior();
            OnePerson.SetName("Glades"); // OK
            // OnePerson._name = "Sam"; // This causes a error of access level too
            // protected members can not be accessed by external scopes
        }
    }
}

Public

[编辑]

Public的成员可以由任何代码访问。

namespace PublicSample
{
    public class Person
    {
        public string Name;
    }

    public class Entry
    {
        static void Main(string[] args)
        {
            Person BeautifulPerson = new Person();
            BeautifulPerson.Name = "Debora"; // OK, public member can be accessed by other scopes
        }
    }
}


Internal

[编辑]

Internal保护的成员只能由同一个assembly的代码看到并访问。

namespace InternalSample
{
    public class Person
    {
        internal string Name;
    }

    public class Entry
    {
        static void Main(string[] args)
        {
            Person BeautifulPerson = new Person();
            BeautifulPerson.Name = "Debora"; // OK, internal member can be accessed by other 
            // scopes in same assembly supposing that Person is in another assembly, by example a 
            // library, the name cannot be accessed. In another assembly source, this causes an error:
            // BeautifulPerson.Name = "Debora"; // Cannot access internal member
        }
    }
}

Protected Internal

[编辑]

Protected internal的成员可由派生类的代码访问,也可由同一个assembly的代码访问。即它是protected internal.[1]


namespace InternalSample
{
    public class Person
    {
        protected internal string Name;
    }

    public class Entry
    {
        static void Main(string[] args)
        {
            Person BeautifulPerson = new Person();
            BeautifulPerson.Name = "Debora"; // As above...
        }
    }
}

public class Book : InternalSample.Person
{
    static void Main(string[] args)
    {
        InternalSample.Person BeautifulPerson = new InternalSample.Person();
        string aName = BeautifulPerson.Name; // Can be accessed, as Book is derived from Person
    }
}

参考文献

[编辑]
  1. Joe Mayo(2007年4月27日).Type Member Access Modifiers.C# STATION.于2011年8月12日查阅.原文:“Either code from derived type or code in the same assembly. Combination of protected OR internal.”