日期:2014-05-16 浏览次数:21019 次
细节:
(1)可以在命名空间中直接定义的类的访问修饰符只能是internal,public
(2)结构成员(包括嵌套class,struct)只能声明为 public,private,internal(struct不能继承, 所以没有protected)
实例代码:
建立两个项目分别为consoleApplication2 ,consoleApplication3
consoleApplication2中有以下以下文件
Class1.cs, Class2.cs, Class3.cs, namespaceTest.cs, Program.cs
//Class1.cs
namespace ConsoleApplication2 { class Class1 { private class Test1 { public int a = 1; } protected class Test2 { public int b = 2; } public class Test3 { int c = 3; } protected internal class Test4 { int d = 4; } internal class Test5 { int e = 5; } int aa = 1; private int bb = 2; protected int cc = 3; public int dd = 4; internal int ee = 5; protected internal int ff = 6; } }//Class2.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { internal class Class2 { public void print() { Class1 cs = new Class1(); //Console.WriteLine("same assemble use Class2 to test Class1's default field :{0}",cs.aa); //error,default field //Console.WriteLine("same assemble use Class2 to test Class1's private field :{0}",cs.bb); //error,private field //Console.WriteLine("same assemble use Class2 to test Class1's protected field :{0}",cs.cc); //error,proteced filed Console.WriteLine("same assemble use Class2 to test Class1's public field :{0}",cs.dd); //OK, public field Console.WriteLine("same assemble use Class2 to test Class1's internal field :{0}", cs.ee); //OK, internal field Console.WriteLine("same assemble use Class2 to test Class1's protected internal field :{0}", cs.ff); //OK, protected internal field } public void printClass() { Class1 cs = new Class1(); //Class1.Test1 t1 = new Class1.Test1(); //error, private class //Class1.Test2 t2 = new Class1.Test2(); //error, protected class Class1.Test3 t3 = new Class1.Test3(); //OK, public class Class1.Test4 t4 = new Class1.Test4(); //OK, protected inner c