1. 变量属性与继承之间的关系 #include <iostream> using namespace std; class A { public: int x; protected: int y; private: int z; }; class B : public A { // x is public // y is protected // z is not accessible from B }; class C : protected A { // x is protected /
Get from Stackoverflow. The details can easily understand from the below example. class A { public: int x; protected: int y; private: int z; }; class B : public A { // x is public // y is protected // z is not accessible from B }; class C : protected
对访问修饰关键字public, protected, internal and private的说明1.msdn: Internal types or members are accessible only within files in the same assembly.只要在同一个assembly中就能访问,而不是命名空间.如下面的例子,在同一个assembly ClassLibrary2.dll中:namespace ClassLibrary2{ public class Clas
在某处看到一张图,简单明了的说明了三者的关系,很是佩服,遂记录下来. //公有继承 对象访问 成员访问 public --> public Y Y protected --> protected N Y private --> private N N //保护继承 对象访问 成员访问 public --> protected N Y protected --> protected N Y private --> protected N N //私有继承 对象访问 成员访
public和private基本没问题,主要是默认的和protected之间的区别 同一包中默认的和protected一样,所以来看看不同包的情况 看下如下代码,两个类位于不同包: public class Base { int i = 0; } public class Extends extends Base { public void test(){ Extends e = new Extends(); Base b = new Base(); //e.i = 1;//编译无法通过 //b