当前位置:首页 > 开发 > 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# 数据类型

Type ByteLenghtMinMax.NET Framework Typedefau...

C# 跳出foreach循环

在 C# 中,如果你想在 foreach 循环内部提前跳出当前这一轮循环,继续执行下一轮循环,可以使...

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

static void Main() { Thread thre...

C# Browsable(bool)

在编程中(比如常见的 C# 语言在开发 Windows Forms 等应用程序时),Browsabl...

C# 类接口

定义接口是一种抽象类型,它定义了一组方法签名(方法名称、参数列表和返回类型),但没有方法体。接口用于...

C# Byte[]转为Bitmap

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