首页 > 代码库 > [基础重温]C#访问修饰符

[基础重温]C#访问修饰符

public
同一程序集中的任何其他代码或引用该程序集的其他程序集都可以访问该类型或成员。 
private
只有同一类或结构中的代码可以访问该类型或成员。 
protected
只有同一类或结构或者此类的派生类中的代码才可以访问的类型或成员。 
internal
同一程序集中的任何代码都可以访问该类型或成员,但其他程序集中的代码不可以。 
protected internal 
由其声明的程序集或另一个程序集派生的类中任何代码都可访问的类型或成员。  从另一个程序集进行访问必须在类声明中发生,该类声明派生自其中声明受保护的内部元素的类,并且必须通过派生的类类型的实例发生。——MSDN

这都是非常基础内容,对于MSDN解释,public和private非常容易理解;

1.对于protected ,需要理解继承这个概念,表示派生类可以访问基类中protect修饰的方法或者变量,例子如下:

using System;namespace ConsoleApplication6{    class Person    {        protected string Name { get; set; }        protected short Age { get; set; }        protected void Display()        {            Console.WriteLine(string.Format("Name:{0},Age:{1}", Name, Age));        }    }    class Student : Person    {        static void Main(string[] args)        {            Student _xiaoMing = new Student();            _xiaoMing.Name = "Yan";            _xiaoMing.Age = 1;            _xiaoMing.Display();        }    }}

<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>2.对于internal,需要理解程序集这个概念,它表示含一个或多个类型定义文件和资源文件的集合。它允许我们分离可重用类型的逻辑表示和物理表示。

即同一个project中. class或struct,interface如果不加修饰符则默认是internal.但也可以显示声明为internal或public(注:如果class,struct是在一个class中声明的也可以protected,private修饰.也就是说一个class中还可以嵌套的声明一个class)

程序集是一个可重用、可实施版本策略和安全策略的单元。它允许我们将类型和资源划分到不同的文件中,这样程序集的使用者便可以决定将哪些文件打包在一起部署。一旦CLR加载了程序集中包含清单的那个文件,它就可以确定程序集的其它文件中哪些包含了程序正在引用的类型和资源。任何程序集的使用者仅需要知道包含清单的文件名称。文件的划分对使用都是透明的,并且可以在将来改变,同时又不会破坏现有的应用程序的行为。
程序集的特性:
1、程序集定义了可重用的类型。
2、程序集标识有一个版本号。
3、程序集可以包含与之相关的安全信息。

3.对于protected internal ,MSDN上的解释有点生硬;上例子比较清楚

   3.1首先新建控制台应用程序,并且新建两个类,目录以及代码如下:

   image

  

using System;namespace ConsoleApplication6{    public class Person    {        protected string Name { get; set; }        protected short Age { get; set; }        protected internal string Address { get; set; }        protected void Display()        {            Console.WriteLine(string.Format("Name:{0},Age:{1},address:{2}", Name, Age, Address == string.Empty ? "没有赋值或没有访问权限." : Address));        }    }    class Student : Person    {        static void Main(string[] args)        {            Student _xiaoMing = new Student();            _xiaoMing.Name = "Yan";            _xiaoMing.Age = 1;            _xiaoMing.Address = "上海";            _xiaoMing.Display();        }    }}
<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>
注意黄色背景代码;

     3.2 新建第二个控制台应用程序,并引用之前DLL;

    image

    一个类继承实现,如图:

    image

   另外一个类,如图:

    image

通过两个图的代码,可以很容易理该修饰符意义了,有人很好总结一句:“如果是继承关系,无论是不是在同一个程序集里都可以访问,如果不是继承关系,只能在同一个程序集中访问。”


参考资料:

1.http://blog.csdn.net/menglin2010/article/details/6054483

2.http://blog.sina.com.cn/s/blog_59a022d6010164f3.html#abc

3.http://www.cnblogs.com/gaoyuchuanIT/articles/1386963.html

4.http://msdn.microsoft.com/zh-cn/library/ms173121.aspx

希望有所帮助!大笑

[基础重温]C#访问修饰符