C#基础巩固——成员变量修饰符protected 、internal与protected internal
2017-07-08 00:01
393 查看
1.类定义
(1)基类
class Assembly1_MyClass1
{
internal int mem_internal_A; //内部成员
protected int mem_protected_A; //保护成员
protected internal int mem_protected_internal_A; //受保护的内部成员
}
(2)同程序集派生类
class Assembly1_MyClass2 :Assembly1_MyClass1 //同程序集继承
{
}
(3)非同程序集派生类
在程序集2中定义Assembly2_MyClass1继承自程序集1中的Assembly1_MyClass1
class Assembly2_MyClass1 :Assembly1_MyClass1
{
}
2.变量修饰符测试(同程序集)
(1)protected类型
测试1:派生类类内访问
class Assembly1_MyClass2 :Assembly1_MyClass1 //同程序集继承
{
Assembly1_MyClass2 A1C2 =new Assembly1_MyClass2(); //派生类实例化
Assembly1_MyClass1 A1C1 =new Assembly1_MyClass1(); //基类实例化
void Test()
{
A1C2.mem_protected_A=1; //可以访问
A1C1.mem_protected_A = 1; //不可访问
}
}
测试2:基类、派生类类外访问
Assembly1_MyClass1 A1C1 =new Assembly1_MyClass1();//基类实例化
Assembly1_MyClass2 A1C2
= new Assembly1_MyClass2(); //派生类实例化
A1C1.mem_protected_A1=1; //基类外protected修饰的变量不可访问
A1C2.mem_protected_A=1; //派生类外protected修饰的变量不可访问
总结:只有通过派生类类型进行访问时,基类的受保护成员在派生类中才是可访问的。
(2)interal类型
测试1:派生类类内访问
class Assembly1_MyClass2 :Assembly1_MyClass1 //同程序集继承
{
Assembly1_MyClass1 A1C1 =new Assembly1_MyClass1(); //基类实例化
Assembly1_MyClass2 A1C2 =new Assembly1_MyClass2(); //派生类实例化
void Test()
{
A1C1.mem_internal_A = 1; //可以访问
A1C2.mem_internal_A = 1; //可以访问
}
}
测试2:基类、派生类类外访问
Assembly1_MyClass1 A1C1 =new Assembly1_MyClass1();//基类实例化 Assembly1_MyClass2 A1C2
= new Assembly1_MyClass2(); //派生类实例化
A1C1.mem_internal_A=1; //internal修饰的成员变量可访问
A1C2.mem_internal_A = 1; //internal修饰的成员变量可访问
总结:在同程序集下,internal类型修饰的成员变量可以在派生类或基类的类内、类外访问。
(3)protected interal类型
测试1:派生类类内访问
class Assembly1_MyClass2 :Assembly1_MyClass1 //同程序集继承
{
Assembly1_MyClass1 A1C1 =new Assembly1_MyClass1(); //基类实例化
Assembly1_MyClass2 A1C2 =new Assembly1_MyClass2(); //派生类实例化
void Test()
{
A1C1.mem_protected_internal_A=1; //可以访问
A1C2.mem_protected_internal_A = 1; //可以访问
}
}
测试2:基类、派生类类外访问
Assembly1_MyClass1 A1C1 =new Assembly1_MyClass1();//基类实例化
Assembly1_MyClass2 A1C2 =
new Assembly1_MyClass2(); //派生类实例化
A1C1.mem_protected_internal_A = 1; //protect internal 修饰的成员变量可访问
A1C2.mem_protected_internal_A = 1; //protect internal 修饰的成员变量可访问
总结:在同程序集下,protected internal类型修饰的成员变量可以在基类或派生类的类内、类外访问(同程序集下protected internal和internal访问性质相同,此处保留了internal的性质)。
3.变量修饰符测试(非同程序集)
测试1:派生类类内访问
Assembly1_MyClass1 A1C1 =new Assembly1_MyClass1(); //基类变量实例化
Assembly2_MyClass1 A2C1 =new Assembly2_MyClass1(); //派生类变量实例化
void Test()
{
A1C1.mem_protected_internal_A = 1; //protected internal修饰通过基类变量不可访问
A2C1.mem_protected_internal_A = 1; //protected internal修饰通过派生类变量可以访问(体现protected性质)
A1C1.mem_protected_A = 1; //protected修饰通过基类变量不可访问
A2C1.mem_protected_A = 1; //protected修饰通过派生类变量可以访问
}
测试2:基类、派生类类外访问
Assembly1_MyClass1 A1C1 =new Assembly1_MyClass1(); //基类实例化
Assembly2_MyClass1 A2C1 =new Assembly2_MyClass1(); //派生类实例化
void Test()
{
//protected internal修饰通过基类变量不可访问
A1C1.mem_protected_internal_A = 1;
//protected internal修饰通过派生类变量不可以访问(体现protected性质)
A2C1.mem_protected_internal_A = 1; A1C1.mem_protected_A = 1; //protected修饰通过基类变量不可访问
A2C1.mem_protected_A = 1; //protected修饰通过派生类变量不可以访问
}
总结:在非同程序集下,protected internal类型修饰的成员变量可以在派生类的类内通过派生类变量进行访问(与同程序集下的protected修饰的相同,此处保留了protected的性质)。
4.总结
在同程序集下,protected internal与internale相同,体现的是internal的性质,即在派生类类内、类外均可访问继承的基类使用protected
internal修饰的成员变量。
在非同程序集下,protected
internal与protected相同,体现的是protected的性质,即在只有在派生类的类内通过派生类实例的成员变量才能访问继承的基类使用protected修饰的成员变量。
(1)基类
class Assembly1_MyClass1
{
internal int mem_internal_A; //内部成员
protected int mem_protected_A; //保护成员
protected internal int mem_protected_internal_A; //受保护的内部成员
}
(2)同程序集派生类
class Assembly1_MyClass2 :Assembly1_MyClass1 //同程序集继承
{
}
(3)非同程序集派生类
在程序集2中定义Assembly2_MyClass1继承自程序集1中的Assembly1_MyClass1
class Assembly2_MyClass1 :Assembly1_MyClass1
{
}
2.变量修饰符测试(同程序集)
(1)protected类型
测试1:派生类类内访问
class Assembly1_MyClass2 :Assembly1_MyClass1 //同程序集继承
{
Assembly1_MyClass2 A1C2 =new Assembly1_MyClass2(); //派生类实例化
Assembly1_MyClass1 A1C1 =new Assembly1_MyClass1(); //基类实例化
void Test()
{
A1C2.mem_protected_A=1; //可以访问
A1C1.mem_protected_A = 1; //不可访问
}
}
测试2:基类、派生类类外访问
Assembly1_MyClass1 A1C1 =new Assembly1_MyClass1();//基类实例化
Assembly1_MyClass2 A1C2
= new Assembly1_MyClass2(); //派生类实例化
A1C1.mem_protected_A1=1; //基类外protected修饰的变量不可访问
A1C2.mem_protected_A=1; //派生类外protected修饰的变量不可访问
总结:只有通过派生类类型进行访问时,基类的受保护成员在派生类中才是可访问的。
(2)interal类型
测试1:派生类类内访问
class Assembly1_MyClass2 :Assembly1_MyClass1 //同程序集继承
{
Assembly1_MyClass1 A1C1 =new Assembly1_MyClass1(); //基类实例化
Assembly1_MyClass2 A1C2 =new Assembly1_MyClass2(); //派生类实例化
void Test()
{
A1C1.mem_internal_A = 1; //可以访问
A1C2.mem_internal_A = 1; //可以访问
}
}
测试2:基类、派生类类外访问
Assembly1_MyClass1 A1C1 =new Assembly1_MyClass1();//基类实例化 Assembly1_MyClass2 A1C2
= new Assembly1_MyClass2(); //派生类实例化
A1C1.mem_internal_A=1; //internal修饰的成员变量可访问
A1C2.mem_internal_A = 1; //internal修饰的成员变量可访问
总结:在同程序集下,internal类型修饰的成员变量可以在派生类或基类的类内、类外访问。
(3)protected interal类型
测试1:派生类类内访问
class Assembly1_MyClass2 :Assembly1_MyClass1 //同程序集继承
{
Assembly1_MyClass1 A1C1 =new Assembly1_MyClass1(); //基类实例化
Assembly1_MyClass2 A1C2 =new Assembly1_MyClass2(); //派生类实例化
void Test()
{
A1C1.mem_protected_internal_A=1; //可以访问
A1C2.mem_protected_internal_A = 1; //可以访问
}
}
测试2:基类、派生类类外访问
Assembly1_MyClass1 A1C1 =new Assembly1_MyClass1();//基类实例化
Assembly1_MyClass2 A1C2 =
new Assembly1_MyClass2(); //派生类实例化
A1C1.mem_protected_internal_A = 1; //protect internal 修饰的成员变量可访问
A1C2.mem_protected_internal_A = 1; //protect internal 修饰的成员变量可访问
总结:在同程序集下,protected internal类型修饰的成员变量可以在基类或派生类的类内、类外访问(同程序集下protected internal和internal访问性质相同,此处保留了internal的性质)。
3.变量修饰符测试(非同程序集)
测试1:派生类类内访问
Assembly1_MyClass1 A1C1 =new Assembly1_MyClass1(); //基类变量实例化
Assembly2_MyClass1 A2C1 =new Assembly2_MyClass1(); //派生类变量实例化
void Test()
{
A1C1.mem_protected_internal_A = 1; //protected internal修饰通过基类变量不可访问
A2C1.mem_protected_internal_A = 1; //protected internal修饰通过派生类变量可以访问(体现protected性质)
A1C1.mem_protected_A = 1; //protected修饰通过基类变量不可访问
A2C1.mem_protected_A = 1; //protected修饰通过派生类变量可以访问
}
测试2:基类、派生类类外访问
Assembly1_MyClass1 A1C1 =new Assembly1_MyClass1(); //基类实例化
Assembly2_MyClass1 A2C1 =new Assembly2_MyClass1(); //派生类实例化
void Test()
{
//protected internal修饰通过基类变量不可访问
A1C1.mem_protected_internal_A = 1;
//protected internal修饰通过派生类变量不可以访问(体现protected性质)
A2C1.mem_protected_internal_A = 1; A1C1.mem_protected_A = 1; //protected修饰通过基类变量不可访问
A2C1.mem_protected_A = 1; //protected修饰通过派生类变量不可以访问
}
总结:在非同程序集下,protected internal类型修饰的成员变量可以在派生类的类内通过派生类变量进行访问(与同程序集下的protected修饰的相同,此处保留了protected的性质)。
4.总结
在同程序集下,protected internal与internale相同,体现的是internal的性质,即在派生类类内、类外均可访问继承的基类使用protected
internal修饰的成员变量。
在非同程序集下,protected
internal与protected相同,体现的是protected的性质,即在只有在派生类的类内通过派生类实例的成员变量才能访问继承的基类使用protected修饰的成员变量。
相关文章推荐
- 一个C#的问卷!private、 protected、 public、 internal 修饰符的访问权限
- c#访问修饰符之“protected internal”
- c#中的访问修饰符Protected,privet ,public, internal,和internal protected
- C# 访问修饰符 public、private、protectrd、internal、protected internal 访问权限
- java基础巩固系列(三):成员变量反射的Field类
- C#基础知识复习1代码规范-执行流程(c#)-面向对象-引用命名空间-封装-继承-访问修饰符-虚方法-静态成员-多态-抽象类等
- C/C++基础之:类成员变量的修饰public/private/protected的区别
- C# 修饰符的总结 default public private protected internal protectedinternal
- C#基础知识——public、private、protected、internal
- C#共有五种访问修饰符:public、private、protected、internal、protected internal。作用范围如下表:
- c#基础概念之internal 修饰符有什么作用
- c#基础加强--类与成员的访问修饰符
- C#共有五种访问修饰符:public、private、protected、internal、protected internal.
- 一个C#的问卷!private、 protected、 public、 internal 修饰符的访问权限
- C#访问修饰符:public、protected、internal、private
- 黑马程序员—C#访问修饰符 public、private、protected 和 internal
- C#访问修饰符:public、protected、internal、private
- 【原创】关于C#访问修饰符protected internal的说明
- C#中。简述 private、 protected、 public、 internal 修饰符的访问权限
- C#基础-可访问性-public、ptotected、protected internal、internal、private