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.

protected关键字是一个成员访问修饰符。一个protected的成员,一个protected成员,在其所在的类中,可由其派生类的实例访问。

可访问性级别的介绍:

https://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx

protected :Access is limited to the containing class or types derived from the containing class.

protected关键字:只能由包含的类进行访问,或者从包含该成员的类所派生的类进行访问。

疑惑的地方:错误观点我本以为只要是派生类的实例,就可以访问受保护的成员

子类的实例,可以赋值给父类类型的变量。通过父类类型的变量,是不允许访问protected成员的。

http://blogs.msdn.com/b/ericlippert/archive/2005/11/09/why-can-t-i-access-a-protected-member-from-a-derived-class.aspx

A question I got recently was about access to protected methods from a derived class.

Clearly that’s what “protected” means – that you can access it from a derived class.

In that case, why doesn’t this work?

 /// <summary>
/// 有蹄类动物
/// </summary>
class Ungulate
{
protected void Eat()
{
/* whatever */
}
} /// <summary>
/// 长颈鹿
/// </summary>
class Giraffe : Ungulate
{
public static void FeedThem()
{
Giraffe g1 = new Giraffe();
Ungulate g2= new Giraffe();
g1.Eat(); // fine
g2.Eat(); // compile-time error “Cannot access protected member”
}
}

What the heck?

Giraffe is derived from Ungulate, so why can’t it always call the protected method?

To understand, you have to think like the compiler.

The compiler can only reason from the static type information, not from the fact that we know that at runtime    //reason from 根据...进行推论

g2 actually will be a Giraffe. For all the compiler knows from the static type analysis, what we’ve actually got here is

        /// <summary>
/// 有蹄类动物
/// </summary>
class Ungulate
{
protected virtual void Eat()
{
/* whatever */
}
} /// <summary>
/// 斑马
/// </summary>
class Zebra : Ungulate
{
protected override void Eat()
{
/* whatever */
}
} /// <summary>
/// 长颈鹿
/// </summary>
class Giraffe : Ungulate
{
public static void FeedThem()
{
Giraffe g1 = new Giraffe();
Ungulate g2 = new Zebra();
g1.Eat(); // fine
g2.Eat(); // compile-time error “Cannot access protected member”
}
}

We can call Ungulate.Eat legally from Giraffe,

but we can't call the protected method Zebra.Eat from anything except Zebra or a subclass of Zebra.

Since the compiler cannot determine from the static analysis that we are not in this illegal situation, it must flag it as being illegal.

总结,protected成员的使用。

1.在包含该成员的类的内部使用。

2.在派生类的内部使用

3.需要注意的是,派生类的实例对象,想要调用protected成员的时候,不能由第三方进行调用。

派生类的实例对象,可以在包含protected的成员里面,或者子类里面。调用protected成员。

这里的派生类,只有一个层次。

class A

{

protected int Number;

}

class B:A

{

}

class C:B

{

B b = new B();

//如果尝试访问Number的话会提示错误。  protected的成员,必须由类B或者B的派生类才可以访问。

//b.Number=10;

}

在类C中可以有B的实例,但是无法在C中通过B的实例去访问protected成员Number。

因为在C从B进行继承的时候,C中Number的父类是B而不是A。

所以,protected成员,只能在本身包含的类中使用,或者派生类中使用。这里的父类,只包含一个层级。

参考资料

Why Can’t I Access A Protected Member From A Derived Class?
Why Can’t I Access A Protected Member From A Derived Class, Part Two: Why Can I?
Why Can’t I Access A Protected Member From A Derived Class, Part Three
Protected Member Access, Part Four
Protected Semantics, Part Five: More on immutability

Protected Member Access的更多相关文章

  1. leetcode 编译问题:Line x: member access within null pointer of type 'struct TreeNode'

    参考: LEETCODE 中的member access within null pointer of type 'struct ListNode' 解决 leetcode 编译问题:Line x: ...

  2. QT编译错误:member access into incomplete type 'QMouseEvent'

    想在QT程序中使用鼠标事件,添加重载的响应函数,并实现后,一直提示 member access into incomplete type 'QMouseEvent' 既然使用了QMouseEvent类 ...

  3. member access within misaligned address 0x0000002c3931 for type 'struct ListNode‘

    From MWeb 在做leetcode 第2题时使用C语言编写链表时报错 错误复现 错误原因 解决办法 错误复现 报错时的代码如下 /** * Definition for singly-linke ...

  4. protected: C++ access control works on per-class basis, not on per-object basis

    一个很简单的问题: //为什么BASE::foo()中可以直接通过p访问val? 看本记录标题,这个问题困扰了很长一段时间,终于解决class BASE {      private:        ...

  5. 141. 环形链表 LeetCode报错:runtime error: member access within null pointer of type 'struct ListNode'

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  6. member access within misaligned address 0x000000000031 for type 'struct ListNode', which requires 8 byte alignment

    在做LeetCode的two sum题目的时候,提交代码遇到了如题的runtime error,后来在一篇博客上看到了解决方法. 现有如下结构体: struct ListNode { int val; ...

  7. The third column indicates whether subclasses of the class declared outside this package have access to the member.;

    总结1.modifier 改性剂 修饰符 修饰语 调节器access modifier 访问修饰符 存取权限 存取修饰词 存取修饰字官网“can”Access level modifiers dete ...

  8. Access the value of a member expression

    Access the value of a member expression 解答1 You can compile and invoke a lambda expression whose bod ...

  9. 深入浅出OOP(五): C#访问修饰符(Public/Private/Protected/Internal/Sealed/Constants)

    访问修饰符(或者叫访问控制符)是面向对象语言的特性之一,用于对类.类成员函数.类成员变量进行访问控制.同时,访问控制符也是语法保留关键字,用于封装组件. Public, Private, Protec ...

随机推荐

  1. java移位操作符

    <<:左移操作符,右边补0,相当于乘二乘二... >>:右移操作符,左边补符号位(正数补0,负数补1),相当于除二除二... >>>:无符号右移,左边补0,相 ...

  2. JPA学习---第一节:JPA详解

    一.详解 JPA JPA(Java Persistence API)是Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关系映射工具来管理Java应用中的关系数据.他的出现主要是 ...

  3. 对frameset、frame、iframe的js操作

    框架编程概述一个HTML页面可以有一个或多个子框架,这些子框架以<iframe>来标记,用来显示一个独立的HTML页面.这里所讲的框架编程包括框架的自我控制以及框架之间的互相访问,例如从一 ...

  4. socket编程实现HTTP请求

    利用c++语言+socket实现HTTP请求,请求获得的数据效果图如下: HTTP协议的下一层是TCP,根据HTTP协议只需要利用TCP发送下面的数据到达目标主机,目标主机就会发送相应的数据到客户端. ...

  5. php中的性能挖掘

    搞php以后,感觉总是很别扭,因为我觉得php会很慢,因为array普遍,在Key的循环查找不是很浪费性能么!因为我以前搞.net和java,他们是用的大多是寻址和索引方式,而php中太多是使用Key ...

  6. hadoop 数据采样

    http://www.cnblogs.com/xuxm2007/archive/2012/03/04/2379143.html 原文地址如上: 关于Hadoop中的采样器 .为什么要使用采样器 在这个 ...

  7. 安装 SQL SERVER PROFILER

    SQL SERVER 2008 R2 (10.50.40) 版本,安装 SQL SERVER PROFILER:通过 command prompt,使用以下命令:setup.exe /FEATURES ...

  8. Memcached 安装及配置

    下载Memcached.exe 保存到c:\memcached 运行command: 输入 c:\memcached\memcached.exe -d install 回车,安装memcached s ...

  9. Unity3d Detect NetState

    public static bool HasConnection() { System.Net.WebClient client; System.IO.Stream stream; try { usi ...

  10. 配置sql server2012属性 ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.zh-CHS/s10de_5techref/html/6df812ad-4d80-4503-8a23-47719ce85624.htm

    服务与服务器是两个不同的概念,服务器是提供服务的计算机,配置服务器主要是对内存.处理器.安全性等几个方面配置.由于SQL Server 2005服务器的设置参数比较多,这里选一些比较常用的介绍. 配置 ...