类修饰符和访问修饰符
成员访问修饰符:缺省时为private
->private 只有本类可以访问
->public 所有类都可访问(不同项目通过添加引用)
->protected 本类与子类访问(继承关系)
->internal 本程序集(如本项目中)
->protected internal 上述二者只要满足其一,就可访问
类的修饰符:缺省时为internal
->internal:本程序集范围类可访问
->public:所有程序集可访问
示例:
1、第一个程序集中的类和相关成员: 访问本程序集的所有类型,以及各类型中以public修饰的成员
(1)本程序集的第一个命名空间 Serialize namespace Serialize
namespace Serialize { //默认的类型修饰为internal class Person { string name; public string Name { get { return name; } set { name = value; } } int age; public int Age { get { return age; } set { age = value; } } Car mycar; internal Car Mycar { get { return mycar; } set { mycar = value; } } public Person() { mycar = new Car(); } } //默认的类型修饰为internal class Car { string carNo; public string CarNo { get { return carNo; } set { carNo = value; } } string carName; public string CarName { get { return carName; } set { carName = value; } } } //显式的声明为public修饰的类型 public class PublicClassTest { int privateNum; public int PrivateNum { get { return privateNum; } set { if (value<10){ privateNum = value;} } } public int publicNum; public static int publicStaticNum; //构造方法 public PublicClassTest() { } public PublicClassTest(int privateNum,int publicNum) { this.privateNum = privateNum; this.publicNum = publicNum; } } }
(2)本程序集的第二个命名空间 Serialize2 namespace Serialize2
namespace Serialize2 { class TestClass { //字段 Serialize.Person p; //用到命名空间 Serialize中定义的类Person //属性 internal Serialize.Person P { get { return p; } set { p = value;} } //构造函数 public TestClass() { p = new Serialize.Person(); } } }
(3)本程序集中自由访问本程序集的所有(internal或public)类型+各类型中以public修饰的成员
namespace Serialize { class Program { static void Main(string[] args) { //当前类是Program类,在以下相关类的外部,不能访问其私有字段,只能访问其公开的属性 Serialize2.TestClass tc = new Serialize2.TestClass(); tc.P.Name = "张三"; tc.P.Age = 30; tc.P.Mycar.CarNo = "A23211"; tc.P.Mycar.CarName = "Audi"; Console.WriteLine("{0} {1} {2} {3}",tc.P.N ame,tc.P.Age,tc.P.Mycar.CarNo,tc.P.Mycar.CarName); Console.ReadKey(); } } }
2、在程序集外部访问: 访问外部程序集中用public修饰的类型,以及这些类型中以public修饰的成员
namespace _0905 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //演示:只能访问外部程序集中 用public修饰的类型,以及类型中以public修饰的成员 private void button1_Click(object sender, EventArgs e) { Serialize.PublicClassTest.publicStaticNum = 6; Serialize.PublicClassTest newInstance = new Serialize.PublicClassTest(); newInstance.publicNum = 9; Console.WriteLine("{0}\r\n{1}",Serialize.PublicClassTest.publicStaticNum,newInstance.publicNum); newInstance.PrivateNum = 11;//set中做了限制:if (value<10){ privateNum = value;} ,本行输出结果为0 Console.WriteLine(newInstance.PrivateNum); newInstance.PrivateNum = 9; Console.WriteLine(newInstance.PrivateNum); } } }
3、继承外部程序集的类 并调用其构造方法
namespace _0905 { class AddclassTest:Serialize.PublicClassTest { //字段 int sum; public int Sum { get { return sum; } set { sum = value; } } //构造方法 public AddclassTest(int num1,int num2):base(num1,num2) { sum = this.PrivateNum + this.publicNum; } } private void button2_Click(object sender, EventArgs e) { //测试继承外部程序集的类 及构造方法的调用 AddclassTest add = new AddclassTest(7, 9); MessageBox.Show(add.Sum.ToString());//运行结果:16 } }