• C#中的一些访问修饰符


    public :访问不受限制。

     1 class PointTest
     2 {
     3     public int x; 
     4     public int y;
     5 }
     6 
     7 class MainClass4
     8 {
     9     static void Main() 
    10     {
    11         PointTest p = new PointTest();
    12         // Direct access to public members:
    13         p.x = 10;
    14         p.y = 15;
    15         Console.WriteLine("x = {0}, y = {1}", p.x, p.y); 
    16     }
    17 }
    18 // Output: x = 10, y = 15
    View Code

    protected :访问仅限于包含类或从包含类派生的类型。

     1 //只有在通过派生类类型发生访问时,基类的受保护成员在派生类中才是可访问的。
     2 class A
     3 {
     4     protected int x = 123;
     5 }
     6 
     7 class B : A
     8 {
     9     static void Main()
    10     {
    11         A a = new A();
    12         B b = new B();
    13 
    14         // Error CS1540, because x can only be accessed by 
    15         // classes derived from A. 
    16         // a.x = 10;  
    17 
    18         // OK, because this class derives from A.
    19         b.x = 10;
    20     }
    21 }
    View Code

    Internal :访问仅于当前程序集。

     1 //示例包含两个文件:Assembly1.cs 和 Assembly1_a.cs。 第一个文件包含内部基类 BaseClass。 在第二个文件中,实例化 BaseClass 的尝试将产生错误。
     2 // Assembly1.cs
     3 // Compile with: /target:library
     4 internal class BaseClass 
     5 {
     6    public static int intM = 0;
     7 }
     8 -------------------------------------------------------------------
     9 // Assembly1_a.cs
    10 // Compile with: /reference:Assembly1.dll
    11 class TestAccess 
    12 {
    13    static void Main() 
    14    {
    15       BaseClass myBase = new BaseClass();   // CS0122
    16    }
    17 }
    View Code
     1 使用与示例 1 中所用的文件相同的文件,并将 BaseClass 的可访问性级别更改为 public。 还将成员 IntM 的可访问性级别更改为 internal。 在此例中,可以实例化类,但不能访问内部成员。
     2 // Assembly2.cs
     3 // Compile with: /target:library
     4 public class BaseClass 
     5 {
     6    internal static int intM = 0;
     7 }
     8 ------------------------------------------------
     9 // Assembly2_a.cs
    10 // Compile with: /reference:Assembly1.dll
    11 public class TestAccess 
    12 {
    13    static void Main() 
    14    {
    15       BaseClass myBase = new BaseClass();   // Ok.
    16       BaseClass.intM = 444;    // CS0117
    17    }
    18 }
    View Code

    protected internal:访问限制到当前程序集或从包含派生的类型的类别。(从当前程序集或从包含类派生的类型,可以访问具有访问修饰符 protected internal 的类型或成员。

    private :访问仅限于包含类型,是一个成员访问修饰符。 私有访问是允许的最低访问级别。 私有成员只有在声明它们的类和结构体中才是可访问的。

     1 //Employee 类包含两个私有数据成员 name 和 salary。 作为私有成员,它们只能通过成员方法来访问。 添加名为 GetName 和 Salary 的公共方法,以便可以对私有成员进行受控/   //的访   问。 通过公共方法访问 name 成员,而通过公共只读属性访问salary 成员。
     2 class Employee2
     3 {
     4     private string name = "FirstName, LastName";
     5     private double salary = 100.0;
     6 
     7     public string GetName()
     8     {
     9         return name;
    10     }
    11 
    12     public double Salary
    13     {
    14         get { return salary; }
    15     }
    16 }
    17 
    18 class PrivateTest
    19 {
    20     static void Main()
    21     {
    22         Employee2 e = new Employee2();
    23 
    24         // The data members are inaccessible (private), so 
    25         // they can't be accessed like this: 
    26         //    string n = e.name; 
    27         //    double s = e.salary; 
    28 
    29         // 'name' is indirectly accessed via method: 
    30         string n = e.GetName();
    31 
    32         // 'salary' is indirectly accessed via property 
    33         double s = e.Salary;
    34     }
    35 }
    View Code
  • 相关阅读:
    Java 如何将 List 转换为 MAP
    Spring Batch BATCH_JOB_INSTANCE 表不存在错误
    Spring 项目启动测试的时候错误:Unable to acquire JDBC Connection
    Spring JPA 如何进行无参数查询布尔类型
    Spring 数据处理中的事务级别
    Spring 测试运行的时候提示 Unable to find a @SpringBootConfiguration 错误
    Spring Batch 可以在一个 Step 中有多个 Tasklet 吗
    Java 属性文件乱码问题
    Spring JPA 查询的时候提示错 org.hibernate.TransientObjectException
    Spring Batch 事务限制
  • 原文地址:https://www.cnblogs.com/sening/p/4587421.html
Copyright © 2020-2023  润新知