我的学习笔记001--private protected public internal mxx

1、简述 private  protected public internal修饰符的访问权限
private: 私有成员, 在类的内部才可以访问。
protected: 保护成员,该类内部和继承类中可以访问。
public: 公共成员,完全公开,没有访问限制。
internal: 当前程序集内可以访问。

其中前三个一般都能理解我自己也理解的差不多胆是internal确实并不是很容易

下面为在网上搜的示例代码

该示例包含两个文件:Assembly1.csAssembly2.cs。第一个文件包含内部基类 BaseClass。在第二个文件中,实例化BaseClass 的尝试将产生错误。

 
// Assembly1.cs
// compile with: /target:library
internal class BaseClass 
{
   public static int intM = 0;
}
 
// Assembly1_a.cs
// compile with: /reference:Assembly1.dll
class TestAccess 
{
   static void Main() 
   {
      BaseClass myBase = new BaseClass();   // CS0122
   }
}

在此示例中,使用与示例 1 中所用的文件相同的文件,并将 BaseClass 的可访问性级别更改为 public。还将成员IntM 的可访问性级别更改为 internal。在此例中,您可以实例化类,但不能访问内部成员。

 
// Assembly2.cs
// compile with: /target:library
public class BaseClass 
{
   internal static int intM = 0;
}
 
// Assembly2_a.cs
// compile with: /reference:Assembly1.dll
public class TestAccess 
{
   static void Main() 
   {
      BaseClass myBase = new BaseClass();   // Ok.
      BaseClass.intM = 444;    // CS0117
   }
}

internal关键字是类型和类型成员的访问修饰符。只有在同一个程序集的文件中,内部类型或者是成员才可以访问。这是msdn上对internal的描述。只有这么一句话,但是具体怎么理解呢?类型就是enum(枚举类型),class(类),interface(接口),struct(结构)等类型。类型成员如函数,成员变量等。那么什么是程序集呢?根据msdn上通俗易懂的解释就是,一个完整的.exe或者是.dll文件就是一个程序集,一般伴随着exe程序集产生的还有一个程序集清单,.exe.config文件。下面我就用一个例子来说明“internal关键字是类型和类型成员的访问修饰符。只有在同一个程序集的文件中,内部类型或者是成员才可以访问”。

首先创建一个visual C#的class libiary项目,在该项目中创建Class1,Class2,Class3,Class 4,Class5如下

public class Class1
{
protected internal string OtherDll="This is Other dll protected internal";
protected string Otherstr = "This is other dll protected";
internal string OtherDll1 = "This is Other dll internal";
}

public class Class2 : Class1
{
public string GetInternal()
{
return this.OtherDll;
}
public string GetProtectedInternal()
{
return this.OtherDll1;
}
public string GetProtected()
{
return this.Otherstr;
}
}

internal class Class3
{
protected string str1 = "this is a test internal class protected attribute";
public string str2 = "This is a test internal class public sttribute";
internal string str3 = "This is a test internal class internal attribute";
}

class Class4 : Class3
{
public string GetInternal()
{
return this.str3;
}
public string GetProtected()
{
return this.str1;
}
public string Getpublic()
{
return this.str2;
}
}

public class Class5
{
Class1 cla1 = new Class1();
Class3 cls3 = new Class3();
public string GetInternal()
{
return cla1.OtherDll;
}
public string GetProtectedInternal()
{
return cla1.OtherDll1;
}
public string Getpublic()
{
return cls3.str2;
}
public string GetInternal1()
{
return cls3.str3;
}
}

现在已经有用internal修饰的类型和类型成员了。

1.从Class1,Class2和Class5中我们可以看到在同一个程序集中(因为Class1,Class2和Class5都是在同一个dll文件中的),internal修饰的类型成员就和public修饰的一样可以被子类调用也可以被被其他的类型调用,而protected internal则和protected等价,不能被非子类调用。

C#中的internal关键字学习

图1 在继承类中internal修饰类型成员

C#中的internal关键字学习

图2 在其他类中的internal修饰类型成员

2.从Class3,Class4和Class5中,我们可以看到internal修饰的类型就和public修饰的一样,protected internal则和protected等价。

C#中的internal关键字学习

图3 internal修饰的继承类

C#中的internal关键字学习

图4在其他类中调用internal修饰的类

在不同程序集中调用internal修饰的类型和类型成员。新建一个winform项目,在该项目中添加对上面创建的dll文件---TestInternallib.dLL。

1.internal修饰的类型

C#中的internal关键字学习

图5 internal修饰的class不可见,只有Class1和Class2

2.intrnal修饰的类型成员的引用

C#中的internal关键字学习

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值