5 Access Modifiers of C#

5 Access Modifiers of C#

1, public public

public allows a class to expose its member variables and member functions to other functions and objects. Any public members can be accessed by external classes.

class Person
{
    
    
    public string name;
    public int age;

    public void SayHello()
    {
    
    
        Console.WriteLine("大家好,我叫{0},今年{1}岁了。", name, age);
    }
}

class Program
{
    
    
    static void Main(string[] args)
    {
    
    
        Person p = new Person();
        p.name = "李华";
        p.age = 18;
        p.SayHello();//调用输出:大家好,我叫李华,今年18岁了。
        Console.ReadKey();
    }
}

2, private private

private allows 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 member variables. Subclasses cannot access private member variables and member functions, even instances of the class cannot access its private members.

class Person
{
    
    
	//通过普通属性保护私有字段,使类的外部不能访问,却可以修改和获取。
    private string _name;
    public string Name
    {
    
    
        get {
    
     return _name; }
        set {
    
     _name = value; }
    }

    private int _age;
    public int Age
    {
    
    
        get {
    
     return _age; }
        set {
    
     _age = value; }
    }

    public void SayHello()
    {
    
    
    	//同一个类中的函数能够访问它的私有成员变量
        Console.WriteLine("大家好,我叫{0},今年{1}岁了", _name, _age);
    }
}
class Program
{
    
    
    static void Main(string[] args)
    {
    
    
        Person p = new Person();
        p.Name = "李华";
        p.Age = 20;
        p.SayHello();//调用输出:大家好,我叫李华,今年20岁了
        Console.ReadKey();
    }
}

3. protected protected

protected only allows functions in the same class to access its protected members, and its subclasses to access its member variables and member functions, which helps to achieve inheritance.

class Person
{
    
    
    private string _name;

    public string Name
    {
    
    
        get {
    
     return _name; }
        set {
    
     _name = value; }
    }

    protected string idCardNo;//受保护的成员变量

    public virtual void SayHello()
    {
    
    
        Console.WriteLine("大家好,我是人类");
    }
}
class Student:Person
{
    
    
    public Student()
    {
    
    
    	//随机一个序列编号,赋值给父类受保护的成员变量
        idCardNo = Guid.NewGuid().ToString();
    }

    public override void SayHello()
    {
    
    
    	//子类不能访问父类私有变量成员,但是可以访问父类受保护成员。
        Console.WriteLine("大家好,我叫{0},我的身份证号码:{1} 。", Name, idCardNo);
    }
}

class Program
{
    
    
    static void Main(string[] args)
    {
    
    
        Person ps = new Student();
        ps.Name = "李华";
        ps.SayHello();//调用输出:大家好,我叫李华,我的身份证号码:4208f78b-253b-4592-b554-08735b3799b0 。
        Console.ReadKey();
    }
}

4. Internal internal access

internal allows access by classes within the same project. The permissions of internal and public are the same in the same project. Internal cannot be accessed by other projects, but public can be accessed after adding a reference.

Referenced project code:

using System;

namespace _04internal
{
    
    
    public class Person
    {
    
    
        private string _name;

        public string Name
        {
    
    
            get {
    
     return _name; }
            set {
    
     _name = value; }
        }
		
		//只能同一个类和子类访问,包括其他项目的子类访问
        protected string idCardNo;

        public virtual void SayHello()
        {
    
    
            Console.WriteLine("大家好,我是人类");
        }
    }
    
    //只能在当前项目(命名空间)中被访问
    internal class Teacher : Person
    {
    
    
        public Teacher()
        {
    
    
            idCardNo = Guid.NewGuid().ToString();
        }
        public override void SayHello()
        {
    
    
            Console.WriteLine("老师说:我叫{0},我的身份证号码:{1} 。", Name, idCardNo);
        }
    }

	//默认访问修饰符 internal
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            Person pt = new Teacher();
            pt.Name = "灵诗";
            pt.SayHello();//调用输出:老师说:我叫灵诗,我的身份证号码:f5f333d2-2017-4ff1-92fa-187f94f0b927 。
            Console.ReadKey();
        }
    }
}

How to reference another project in Visual Studio:

1. Right-click the [Reference] of this project, and select the [Add Reference] option with the left mouse button.
insert image description here
2. Click [Project], check the project to be referenced, and then click OK.
insert image description here
Another project code after adding reference :

using _04internal;//添加引用后,还要用 using 引入命名空间
using System;

namespace test
{
    
    
	//默认访问修饰符 internal ,继承其他命名空间下非internal的类
    class Student:Person
    {
    
    
        public Student(string name)
        {
    
    
            this.Name = name;//可以访问不同命名空间下父类受保护的成员变量
            idCardNo = Guid.NewGuid().ToString();
        }

        public override void SayHello()
        {
    
    
            Console.WriteLine("学生说:我叫{0},我的身份证号码:{1} 。", Name, idCardNo);
        }
    }
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            Person ps = new Student("李华");
            ps.SayHello();//调用输出:学生说:我叫李华,我的身份证号码:3968bfb9-2bcc-47c5-bea4-283c3fc9ea6d 。
            Console.ReadKey();
        }
    }
}

5、protected internal (internal protected)

protected internal allows classes in the same project to access, and also allows subclasses in other projects to access its member variables and member functions.

Referenced project code:

using System;

namespace _05protectedinternal
{
    
    
    public class Person
    {
    
    
        protected internal string _name;//受保护内部访问字段
        internal string _idCardNo;//内部访问字段
        public Person(string name)
        {
    
    
            _name = name;
            _idCardNo = Guid.NewGuid().ToString();
        }

        public virtual void SayHello()
        {
    
    
            Console.WriteLine("大家好,我叫{0} ,身份证号码:{1}.", _name, _idCardNo);
        }
    }

    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            Person p = new Person("李华");
            p.SayHello();//调用输出:大家好,我叫李华 ,身份证号码:6bd42c42-4278-4541-a5aa-c1a8512c58af.
            Console.ReadKey();
        }
    }
}

Another project code after adding reference :

using _05protectedinternal;//添加引用后,还要用 using
using System;

namespace test
{
    
    
    class Student:Person
    {
    
    
        public Student(string name)
            :base(name)
        {
    
    

        }

        public override void SayHello()
        {
    
    
            //只能访问另一个项目父类受保护内部成员,不能访问父类内部成员,因此获取不到身份证号码
            Console.WriteLine("大家好,我叫{0}.", _name);
        }
    }
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            Person ps = new Student("李华");
            ps.SayHello();//调用输出:大家好,我叫李华.
            Console.ReadKey();
        }
    }
}
总结:
1、private 只允许在本项目本类中被访问。
2、proteced 只允许在当前类内部以及该类的子类中访问。
3、类的访问修饰符只有 internal(默认)和 public。
4、internal 只能在当前项目中访问。在同一个项目中与 public 的权限一致。
5、protected internal 可以访问跨项目父类的成员变量和函数。两个修饰符位置随意。

Due to the limited energy of the author, some mistakes and omissions will inevitably appear in the article. Experts and netizens are welcome to criticize and correct me.

Guess you like

Origin blog.csdn.net/qq_46051312/article/details/123470829