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

C# protected

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

添加的成员可以是任意类型    ArrayList arra...

C# 一行代码交换变量

int a = 10 ; int b ...

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

static void Main() { Thread thre...

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

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

c# Invalidate、Refresh、Refreshitem、Refreshitems的功能

Invalidate方法功能概述Invalidate方法主要用于使控件的特定区域(整个控件或部分区域...

C# Byte[]转为Image

以下是在 C# 中将byte[](字节数组,通常表示图像的二进制数据)转换为Image类型的常见方法...