zoukankan      html  css  js  c++  java
  • protected(C# 参考)

    protected 关键字是一个成员访问修饰符。受保护成员在它的类中可访问并且可由派生类访问。有关 protected 与其他访问修饰符的比较,请参见可访问性级别

    仅当访问通过派生类类型发生时,基类的受保护成员在派生类中才是可访问的。例如,请看以下代码段:

     
     
    // protected_keyword.cs
    using System;
    class A
    {
        protected int x = 123;
    }
    
    class B : A
    {
        static void Main()
        {
            A a = new A();
            B b = new B();
    
            // Error CS1540, because x can only be accessed by
            // classes derived from A.
            // a.x = 10; 
            
            // OK, because this class derives from A.
            b.x = 10;   
        }
    }
    

    语句 a.x =10 将生成错误,因为 A 不是从 B 派生的。

    结构成员无法受保护,因为无法继承结构。

    在此示例中,类 DerivedPoint 从 Point 派生;因此,可以从该派生类直接访问基类的受保护成员。

     
     
    // protected_keyword_2.cs
    using System;
    class Point 
    {
        protected int x;     protected int y;
    }
    
    class DerivedPoint: Point 
    {
        static void Main() 
        {
            DerivedPoint dp = new DerivedPoint();
    
            // Direct access to protected members:
            dp.x = 10;        dp.y = 15;
            Console.WriteLine("x = {0}, y = {1}", dp.x, dp.y); 
        }
    }
    

    输出

    x = 10, y = 15
  • 相关阅读:
    JQuery使用总结
    JS应用总结
    Base64数据转成Excel,并处理Excel的格式
    HTTP压缩
    谷歌开发工具解析
    .Net LIst排重
    MySql日志系统
    .Net生成PDF流
    Mysql MVCC
    JAVA期末综合课程设计
  • 原文地址:https://www.cnblogs.com/xpvincent/p/3187639.html
Copyright © 2011-2022 走看看