当前位置:首页 > 开发 > C# > 正文内容

C# protected

C#5年前 (2021-12-20)

官方:

只有在通过派生类类型发生访问时,基类的受保护成员在派生类中才是可访问的。 

简单理解:

        当一个类作为派生类被访问时,由protected修饰的成员才可使用。  

失败的

    class Program
    {

        static void Main(string[] args)
        {
            CCon cCon = new CCon();
            Console.WriteLine(cCon.name);
            Console.WriteLine(cCon.age);    //  CCon.age”不可访问,因为它具有一定的保护级别
            Console.Read();
        }
    }
    class CCon
    {
        public string name = "AP000";
        protected int age = 23;
    }

成功的

    class Program : CCon
    {
        static void Main(string[] args)
        {
            Program program = new Program();
            Console.WriteLine(program.name);
            Console.WriteLine(program.age);
            Console.Read();
        }

    }

    class CCon
    {
        public string name = "AP000";
        protected int age = 23;
    }


转载请注明出处。

本文链接:http://pythonopen.com/?id=174

相关文章

C# ListView 虚拟化加载百万数据

private void Form1_Load(object send...

C# 可空参数

using System; using System.Runtime.Inte...

C# 缩减代码量的一些方式

static void Main() { Thread thre...

C# [OnPaint]和[OnPaintBackground]的区别

OnPaint和OnPaintBackground的主要功能区别OnPaint:OnPaint方法主...

C# BackgroundWorker的例子

以下是一个使用 BackgroundWorker 组件在 C# 中实现后台执行任务,同时在主线程更新...

C# System.IO.Path

System.IO.Path.GetExtension返回指定的路径字符串的扩展名。string&n...