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

C# protected

C#3年前 (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

相关文章

DotfuscatorPro使用教程

DotfuscatorPro使用教程

1首次使用,添加反编译工具路径ILASM_v4.0.30319C:\Windows\Microsof...

C# 获取文件图标

string file = @"C:\Windows\exp...

C# BackgroundWorker

1.概述BackgroundWorker是一个在 WinForms 应用程序中用于简化在后台线程执行...

C# System.IO.Path

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

C# BackgroundWorker,在DoWork里更新控件内容

一般情况下不可以直接在BackgroundWorker的DoWork事件中更新 UI 控件在Back...

C# Byte[]转为Bitmap

在 C# 中,可以使用System.Drawing命名空间下的相关类将byte[]类型的数据转换为B...