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. iOS开发应用学习笔记

    一.iOS应用设计 1. 参考资料: 解读iPhone平台的一些优秀设计思路 iPhone App的特点及基本设计方法 Mobile UI design and Developer 2. 用户对iPh ...

  2. LoadRunner 学习笔记(1)性能测试常见术语

    并发用户数据:与服务器进行交互的在线用户数量 请求响应时间:从Client端发出请求到得到响应的整个时间 一般包括网络响应时间 + server的响应时间 事务请求响应时间:完成这个事务所用的时间 这 ...

  3. windows下编译ffmpeg

    windows 编译ffmpeg 搞过很多次,每次总是磕磕碰碰,从头到尾不能一直顺利,浪费一些时间.终究起原因,都是当时记得,过段时间就忘了.好记性不如烂笔头,大好周末晚上,闲暇无事,记录最近一次编译 ...

  4. 3142:[HNOI2013]数列 - BZOJ

    题目描述 Description 小T最近在学着买股票,他得到内部消息:F公司的股票将会疯涨. 股票每天的价格已知是正整数,并且由于客观上的原因,最多只能为N.在疯涨的K天中小T观察到:除第一天外每天 ...

  5. Codeforces Round #303 (Div. 2) E. Paths and Trees 最短路+贪心

    题目链接: 题目 E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes inputs ...

  6. 51nod 1640 天气晴朗的魔法 最小生成树

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1640 题解: 先求最小生成树,记录最大边. 然后求最大生成树 ...

  7. 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)

    // test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  8. 【C# 反射泛型】

    C# 反射泛型 摘自:http://www.itwis.com/html/net/c/20110411/10175.html C#泛型反射和普通反射的区别,泛型反射和普通反射的区别就是泛型参数的处理上 ...

  9. [Unity3D+算法]一小时做个2048

    原地址:http://blog.csdn.net/dingxiaowei2013/article/details/36462749 048是继FlappyBird之后另一个比较热的轻量级的手游,简单易 ...

  10. Understanding and Using Servlet Filters

    Overview of How Filters Work This section provides an overview of the following topics: How the Serv ...