c# 类的public private internal protected的区别
来源:互联网 发布:对称加密 java 编辑:程序博客网 时间:2024/05/24 06:15
在C#中,有如下几种修饰符,分别是public,protected,internal,private。先来看一下C# 4th Edition中的说明:
public
No restrictions. Members marked public are visible to any method of any class.
private
The members in class A that are marked private are accessible only to methods of class A.
protected
The members in class A that are marked protected are accessible to methods of class A and also to methods of classes derived from class A.
internal
The members in class A that are marked internal are accessible to methods of any class in A's assembly.
protected internal
The members in class A that are marked protected internal are accessible to methods of class A, to methods of classes derived from class A, and also to any class in A's assembly. This is effectively protected OR internal. (There is no concept of protected AND internal.)
public,protected,private这三个就不说了,来看一下internal和protected/internal的用法。
假设有类A如下:
public class A
{
protected int prot = 0;
public int publ = 0;
public void pub_func()
{ }
protected void prot_func()
{ }
internal void inter_func()
{ }
protected internal void prot_inter_func()
{ }
}
public class Tester :A {
static void Main( ) {
A a= new A( );
//a.inter_func();
//a.prot_inter_func();
//a.pub_func();//this.inter_func();
//this.prot_inter_func();
//this.pub_func();
}
}
如果在同一个程序集中,假如有一个类A,其它类中想要使用A中的属性或方法的话,不管是new方法产生的还是继承产生的,都可以访问到protected internal和internal的方法。
但是如果A类不是在同一个程序集中,就有很大的区别了,首先,你用new方法产生对象中,不可以使用protected internal或internal来访问。继承的方式可以访问到protected internal,但不可访问internal。
当你写的一些类,不想让外部访问,只想让它的子类范围内访问的话,使用protected internal可以起到很好的保护作用。
c# 类的public private internal protected的区别:
public 修饰的类,可以在整个系统的任意地方调用,是完全公开的.
private 相反的,只能在类内部调用.任何实例,无法调用private调用.
internal 仅为同项目(这里的项目是只单独的项目,而不是整个解决方案)调用,按照我的理解,应该是和java的friendly一样的效果.
protected 自己及自己的子类可以调用
- c# 类的public private internal protected的区别
- c# 类的public private internal protected的区别
- c# 类的public private internal protected的区别
- c# 类的public private internal protected的区别
- c# 修饰词public, protected, private,internal,protected的区别
- c# 修饰词public, protected, private,internal,protected的区别
- c# 修饰词public, protected, private,internal,protected的区别
- c#中public,private,protected,internal的区别
- c#中public,private,protected,internal的区别
- c#中public,private,protected,internal的区别
- c#中public,private,protected,internal的区别
- c#中public,private,protected,internal的区别
- [C#基础知识]之private、protected、public和internal的区别
- c# 修饰词public, protected, private,internal的区别
- c#中public private protected internal 的区别
- 关于C#代码中的private protected public internal的区别
- c#中public,private,protected,internal的区别
- c#中public,private,protected,internal的区别
- 在CListCtrl之间及CListCtrl内部实现拖放
- 杨辉三角(C#)
- JS取得fckeditor的值
- Linux下Telnet的安装与运行
- Java简介(转)
- c# 类的public private internal protected的区别
- 动态语句长度超过32K的解决方法
- 巴菲特:我更喜欢简单一点
- CSDN下载频道积分补救
- WLAN主要技术标准与进展介绍
- javascript中定义Table对象
- flex 总结
- 浏览器使用情况 IE6依旧是老大
- 加快生成NK的一些注意事项