c#访问修饰符

时间:2020-12-26 02:55:52

声明的可访问性

含义

public

访问不受限制。

protected

访问仅限于包含类或从包含类派生的类型。

internal

访问仅限于当前程序集。

protected internal

访问仅限于从包含类派生的当前程序集或类型。

private

访问仅限于包含类型。

 以下是例子程序,根据例子实验可以笔记深入的了解访问修饰符的含义:

同一程序集(同一个工程下):

 public class BaseCls
    {
        private int private_a = 0;
        protected int protected_b = 1;
        internal int internal_c = 2;
        protected internal int pi_d = 3;
        public int public_e = 4;
        private int get_private()
        {
            return 5;
        }
        protected int get_protected()
        {
            return 6;
        }
        internal int get_internal()
        {
            return 7;
        }
        protected internal int get_pi()
        {
            return 8;
        }
        public int get_public()
        {
            return 9;
        }       
    }
    public class inheritCls : BaseCls
    {
        public inheritCls()
        {
            Console.WriteLine("同一程序集中继承类访问权限测试");
            Console.WriteLine("private,intenal字段不被继承,protected,protected intenal,public被继承");
            Console.WriteLine(this.protected_b);
            Console.WriteLine(this.pi_d);
            Console.WriteLine(this.public_e);
            Console.WriteLine("private方法不被继承,protected,internal,protected intenal,public被继承");
            Console.WriteLine(this.get_protected());
            Console.WriteLine(this.get_internal());
            Console.WriteLine(this.get_pi());
            Console.WriteLine(this.get_public());
        }
    }
    public class Test
    {
        public Test()
        {
            Console.WriteLine("同一程序集中类的访问权限测试");
            BaseCls baseCls = new BaseCls();
            Console.WriteLine(baseCls.pi_d);
            Console.WriteLine(baseCls.public_e);
            Console.WriteLine(baseCls.get_internal());
            Console.WriteLine(baseCls.get_pi());
            Console.WriteLine(baseCls.get_public());
            inheritCls cls = new inheritCls();
            Console.WriteLine(cls.pi_d);
            Console.WriteLine(cls.public_e);
            Console.WriteLine(cls.get_internal());
            Console.WriteLine(cls.get_pi());
            Console.WriteLine(cls.get_public());
        }
    }


再新建一个工程:

class ClassTT : BaseCls
    {
        public ClassTT()
        {
            Console.WriteLine("不同一程序集中继承类访问权限测试");
            Console.WriteLine("private,intenal字段不被继承,protected,protected intenal,public被继承");
            Console.WriteLine(this.protected_b);
            Console.WriteLine(this.pi_d);
            Console.WriteLine(this.public_e);
            Console.WriteLine("private,intenal方法不被继承,protected,internal,protected intenal,public被继承");
            Console.WriteLine(this.get_protected());
            Console.WriteLine(this.get_pi());
            Console.WriteLine(this.get_public());
        }
    }

    public class TestTT
    {
        public TestTT()
        {
            Console.WriteLine("不同程序集中类的访问权限测试");
            BaseCls baseCls = new BaseCls();
            //Console.WriteLine(baseCls.pi_d);
            Console.WriteLine(baseCls.public_e);
            //Console.WriteLine(baseCls.get_internal());
            //Console.WriteLine(baseCls.get_pi());
            //Console.WriteLine(baseCls.get_public());
            inheritCls cls = new inheritCls();
            //Console.WriteLine(cls.pi_d);
            Console.WriteLine(cls.public_e);
            //Console.WriteLine(cls.get_internal());
            //Console.WriteLine(cls.get_pi());
            //Console.WriteLine(cls.get_public());
            ClassTT clsTT = new ClassTT();
            Console.WriteLine(clsTT.public_e);
            Console.WriteLine(clsTT.get_public());
        }
    }
}


以上的例子主要说明同一程序集和不同程序集之间的继承和调用时的访问权限。总的来说多动手来理解。