C#编程之C#中的一些访问修饰符
小标 2019-03-13 来源 : 阅读 1189 评论 0

摘要:本文主要向大家介绍了C#编程之C#中的一些访问修饰符,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。

本文主要向大家介绍了C#编程之C#中的一些访问修饰符,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。

C#编程之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

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C#.NET频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程