.NET关于访问修饰符作用范围的测试

时间:2021-07-09 16:35:06

如题,一个很基础的问题。先说结论,再贴代码。

结论:

    类有public和internal 两种访问修饰符,默认是internal.类不能继承静态类或密封类,不能继承比它访问级别低的类;在两个项目之间,类只能继承public类.

   在类的内部的修饰符有这么5个:public,private,protected,internal,protected internal  ,前2个举世皆知,重点说后3个。在同一个项目内,类的继承3者皆可访问,在2个项目之间,类的继承3者中internal类型的不可访问.

完毕,上代码:

.NET关于访问修饰符作用范围的测试.NET关于访问修饰符作用范围的测试
using System;

namespace ClassLibrary1
{
    public class Class1
    {
        int mi;
        private int pr;
        protected int po;
        internal int i ;
        protected internal int pi;
        public int pb;
    }

    class Class2
    {
        internal int i;
        protected internal int pi;
        protected int p;
    }

    internal class Class3
    {
        internal int i;
        protected internal int pi;
        protected int p;
    }

    public class Class4:Class1
    {
        public Class4()
        {
            int i = base.i;
        }

        public void GetId()
        {
            int po = base.po;
        }
    }

    class Class42 : Class2
    {
        public Class42()
        {
            int i = base.i;
        }

        public void GetId()
        {
            int po = base.i+base.p+base.pi;
        }
    }

    class Class422 : Class3
    {
        public Class422()
        {
            int i = base.i;
        }

        public void GetId()
        {
            int po = base.i + base.p + base.pi;
        }
    }

    //错误    1    可访问性不一致: 基类“ClassLibrary1.Class2”比类“ClassLibrary1.Class43”的可访问性低    d:\visual studio 2010\Projects\ClassLibrary1\ClassLibrary1\Class1.cs    58    18    ClassLibrary1
    //public class Class43 : Class2
    //{
    //    public Class43()
    //    {
    //        int i = base.i;
    //    }

    //    public void GetId()
    //    {
    //        int po = base.i + base.p + base.pi;
    //    }
    //}



    internal static class Class5
    {
        internal static int is2;
        private static int prs;
        static int ns;
        public static int pbs;
    }

    public static class Class6
    {
        internal static int is2;
        private static int prs;
        static int ns;
        public static int pbs;
    }
}
项目1
.NET关于访问修饰符作用范围的测试.NET关于访问修饰符作用范围的测试
using System;
using ClassLibrary1;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
         
        }

        public void GetId()
        {
           X1 x=new X1();
           int i = x.pb;//只能访问public
        }
    }

    public class X1 : Class1
    {
        public void GetId()
        {
            int i = base.pb;//public int pb;
            i = base.pi;//protected internal int pi;
            i = base.po;//protected int po;
        }
    }

    ////错误    1    “ConsoleApplication1.X2”: 无法从静态类“ClassLibrary1.Class6”派生    d:\visual studio 2010\Projects\ClassLibrary1\ConsoleApplication1\Program.cs    22    18    ConsoleApplication1
    //public class X2 : Class6
    //{

    //}

    ////cannot access internal class 'Class2' here
    //public class X3 : Class2
    //{
    //    public void GetId()
    //    {
    //        int i = base.pb;//public int pb;
    //        i = base.pi;//protected internal int pi;
    //        i = base.po;//protected int po;
    //    }
    //}
}
项目2