当前位置:首页 > 开发 > 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://www.pythonopen.com/?id=174

相关文章

在 C# 中实现类似于 Windows 资源管理器的“名称”排序方式

要在 C# 中实现类似于 Windows 资源管理器的“名称”排序方式,你需要考虑以下几点:1. 不...

C# using与多重using

1. using 语句概述在 C# 中,using 语句主要用于确保实现了 IDisposable...

C# i++和++i的区别

核心区别操作顺序            ...

C# 比较两个Image对象是否相同

方法思路基础检查:先检查空引用和图像尺寸像素格式验证:确保两个图像的像素格式相同内存锁定:使用Loc...