protected (C# Reference)
https://msdn.microsoft.com/en-us/library/bcd5672a.aspx
The protected keyword is a member access modifier.
A protected member is accessible within its class and by derived class instances.
For a comparison of protected with the other access modifiers, see Accessibility Levels.
Example
A protected member of a base class is accessible in a derived class only if the access occurs through the derived class type.
For example, consider the following code segment:
class A
{
protected int x = ;
} 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 = ;
}
}
The statement a.x = 10 generates an error because it is made within the static method Main, and not an instance of class B.
Struct members cannot be protected because the struct cannot be inherited.
Example
In this example, the class DerivedPoint is derived from Point.
Therefore, you can access the protected members of the base class directly from the derived class.
class Point
{
protected int x;
protected int y;
} class DerivedPoint: Point
{
static void Main()
{
DerivedPoint dpoint = new DerivedPoint(); // Direct access to protected members:
dpoint.x = ;
dpoint.y = ;
Console.WriteLine("x = {0}, y = {1}", dpoint.x, dpoint.y);
}
}
// Output: x = 10, y = 15
If you change the access levels of x and y to private, the compiler will issue the error messages:
'Point.y' is inaccessible due to its protection level.
'Point.x' is inaccessible due to its protection level.
C# Language Specification
For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.
See Also
C# Reference
C# Programming Guide
C# Keywords
Access Modifiers (C# Reference)
Accessibility Levels (C# Reference)
Modifiers (C# Reference)
public (C# Reference)
private (C# Reference)
internal (C# Reference)
protected (C# Reference)的更多相关文章
- 设计模式之美:Command(命令)
索引 别名 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):直接注入 Receiver 对象,Command 决定调用哪个方法. 实现方式(二):注入 Receiver 的指定方法, ...
- 【Java&Android开源库代码剖析】のAndroid-Universal-Image-Loader-part1
做Android app开发的同学应该都听说过或者用过nostra13的Android-Universal-Image-Loader开源库,它在图片异步加载.缓存和显示等方面提供了强大灵活的框架.之前 ...
- android universal image loader 缓冲原理详解
1. 功能介绍 1.1 Android Universal Image Loader Android Universal Image Loader 是一个强大的.可高度定制的图片缓存,本文简称为UIL ...
- Android -- ImageLoader简析
图片的内存缓存实现 Image-Loader库有一个较完整的内存缓存实现,使用者可以根据需要选择已经实现的策略,也可以定制自己项目中需要的策略. 内存缓存实现代码在memory和memory.impl ...
- Java中的软(弱)引用
一.Java中的强.软.弱.虚引用 在JDK中我们能够看到有一个java.lang.ref的包.这个包中就是Java中实现强.软.弱.虚引用的包,例如以下: PhantomReference 虚引用: ...
- 从设计到实现,一步步教你实现Android-Universal-ImageLoader-缓存
转载请标明出处,本文出自:chaossss的博客 Android-Universal-ImageLoader Github 地址 Cache 我们要对图片进行缓存.有两种方式:内存缓存和本地缓存. 这 ...
- Android-Universal-Image-Loader学习笔记(3)--内存缓存
前面的两篇博客写了文件缓存.如今说说Android-Universal-Image-Loader的内存缓存.该内存缓存涉及到的类如图所看到的 这些类的继承关系例如以下图所看到的: 如同文件缓存一样,内 ...
- 深入解析开源项目之Universal-Image-Loader(二)内存---缓存篇
珍惜作者劳动成果,如需转载,请注明出处. http://blog.csdn.net/zhengzechuan91/article/details/50292871 Universal-Image-Lo ...
- 网络拓扑实例之RRPP单环(五)
组网图形 RRPP简介 在城域网和企业网的网络规划以及实际组网应用中大多会采用环网结构来提高网络的可靠性.采用环网结构的好处是:当环上任意一个节点或节点之间的链路发生故障,都可以将数据流量切换到备份链 ...
随机推荐
- redis源码(一):为redis添加自己的列表类型
本文档分为三大部分: 环境介绍与效果演示 redis接收命令到返回数据的执行逻辑 代码实现 文档的重点和难点在第三部分,完全阅读本文档需要读者具备基本的c语言和数据结构知识. 环境介绍和效果演示环境介 ...
- wpf 自定义Button按钮
创建ButtonEx类 public class ButtonEx : Button { static ButtonEx() { DefaultStyleKeyProperty.OverrideMet ...
- A4. JVM 内存分配及回收策略
[概述] Java 技术体系中所提倡的自动内存管理最终可以归结为自动化地解决两个问题:给对象分配内存以及回收分配给对象的内存. 对象的内存分配,往大方向讲,就是在堆上分配,对象主要分配在新生代的 Ed ...
- tab下拉显示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- 『 Luogu P3205 』 HNOI2010 合唱队
解题思路 设置两个二维数组 $f$ 和 $g$,含义如下. $f[l][r]$ 表示在期望得到的队形中 $l\rightarrow r$ 这段区间初始队形排列的方案数,并且最后一个加入进去的是第 $l ...
- ZOJ - 3987 - Numbers (大数 + 贪心)
参考自:https://blog.csdn.net/u013534123/article/details/78484494 题意: 给出两个数字n,m,把n分成m份,使得以下最小 思路: 或运算只有0 ...
- python OOP(2)
调用方法有两种形式 实例调用 直接调用后序参数即可 类调用 调用时需要先加上实例 示例 class test1: def pt(self,txt): #定义函数 test1.txt=txt print ...
- DTD 文件的引入
MyBatis 有两种配置文件:核心配置文件(mybatis- config.xml)和 SQL 映射文件(mapper.xml).这两种配置文件都需要手动引入各自的 DTD 文件(mybatis-3 ...
- Servlet过滤器的使用
Servlet过滤器的使用 制作人:全心全意 Servlet过滤器:Servlet过滤器与Servlet十分相似,但它具有拦截客户端请求的功能,Servlet过滤器可以改变请求中的内容,来满足实际开发 ...
- [luoguP1316] 丢瓶盖(二分答案)
传送门 二分答案再判断即可 ——代码 #include <cstdio> #include <iostream> #include <algorithm> #def ...