您的位置:首页 > 编程语言 > C#

C#学习日记21----封装 与 访问修饰符

2015-10-12 18:28 549 查看

 

封装:

       被定义为"把一个或多个项目封闭在一个物理的或者逻辑的包中"。在面向对象程序设计方法论中,封装是为了防止对实现细节的访问。也就是把里面实现的细节包起来,这样很复杂的逻辑经过包装之后给别人使用就很方便,别人不需要了解里面是如何实现的,只要传入所需要的参数就可以得到想要的结果。封装使用访问修饰符 来实现。一个访问修饰符 定义了一个类成员的范围和可见性。

 

访问修饰符:

            在
类class的声明与定义  中我简略的介绍了访问修饰符,不是很具体,这里我们深入学习访问修饰符;

              在C#中访问修饰符有5个public、private、protected、internal、protected internal;

public修饰符

         public 访问修饰符允许一个类将其成员变量和成员函数暴露给其他的函数和对象。任何公有成员可以被外部的类访问。(不限制对成员的访问,子类可以,对象也可以)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
class student
{
public int Chinese=80;
public int Math=67;
public int English=45;
//定义一个方法
public int total()
{
int sum = 0;
sum = Chinese + Math + English;
return sum;
}
}
class teacher
{
static void Main(string[] args)
{
student stu = new student();  //实例化一个对象
Console.WriteLine("Chinese = {0}\nMath = {1}\nEnglish = {2}\nTotal = {3}",stu.Chinese,stu.Math,stu.English,stu.total());
}
}
}

输出结果:



在上面的例子中student中的所有成员都是public类型访问,所以他的实例化对象 stu 能够访问所有成员。

 

private修饰符

       Private 访问修饰符允许一个类将其成员变量和成员方法对他的对象进行隐藏。只有同一个类中的方法可以访问它的私有成员。即使是类的实例也不能访问它的私有成员 (不允许他的子类或对象访问)。

我们将上面的例子利用下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
class student
{
private int Chinese=80;
private int Math=67;
public int English=45;

private int total()
{
int sum = 0;
sum = Chinese + Math + English;
return sum;
}
}
class teacher
{
static void Main(string[] args)
{
student stu = new student();
int Chinese = stu.Chinese;     //出错了,不可访问(private)

int English = stu.English;   //可以访问 (public)

int total = stu.total();  // 不可访问 private 方法
}
}
}


由于在student类中对变量 Chinese、Math 方法 Total 有private 访问限制,所以无法通过对象stu 访问

 

protected修饰符

           Protected 访问修饰符允许子类访问它的基类的成员变量和成员函数。这样有助于实现继承。(不允许对象访问,允许类中的方法访问,允许子类访问)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
class student
{
//定义protected 类型保护
protected int Chinese=80;
protected int Math=67;
protected int English=45;

protected int total()
{
int sum = 0;
sum = Chinese + Math + English;
return sum;
}
}
class teacher:student  //teacher 继承student类
{
static void Main(string[] args)
{
teacher teac = new teacher();   //创建子类 teacher 对象
//全部成功访问
int Chinese = teac.Chinese;

int English = teac.English;

int total = teac.total();

}
}
}


internal修饰符

         Internal 访问修饰符允许一个类将其成员变量和成员方法暴露给当前程序集中(当前项目)的其他方法和对象。换句话说,带有 internal 访问修饰符的任何成员可以被定义在该成员所定义的程序集(项目)内的任何类或方法访问。

      要学习internal修饰符,我们就得先学习使用Vs2010创建多个工程以及工程与工程的引用方法:

先创建一个工程文件:命名为 Test_internal

 


点击确定后,找到解决方案管理器:



右键 解决方案“Test_internal”(1个项目) --->添加--->新建项目---->visual C#--->控制台;  在名称栏中我们命名为Class_internal 确定;这样我们就创建了2 个项目(程序集)



 

     其实我们上面输入的名称就是我们定义的命名空间的名称,打开代码Class_internal的命名空间是 namespace Class_internal,  Test_internal 的命名空间是namespace Test_internal,我们在一个工程里通过using 命名空间来引用另一个工程的内容,不过在此之前我们还得 
在  “引用” 列表中添加被引用的工程路径:

   右键 “引用”--->添加引用--->项目。 在项目名称列表中找到要引用的项目名称这里我们选 Class_internal 单击确定,引用列表中就会多出 Class_internal 引用成功



 

 我们打开Class_internal中的program.cs编辑以下代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Class_internal
{
public class student
{  //定义2个 internal 访问类型与一个public访问类型
internal int Chinese=80;
internal int Math=70;
public int English=56;
//定义一个internal 访问类型方法
internal int Total()
{
int sum = 0;
sum = Chinese + Math + English;
return sum;
}
static void Main(string[] args)
{

}
}

}


然后在Test_internal中编辑以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Class_internal;  //引用命名空间

namespace Test_internal
{
class Program
{
static void Main(string[] args)
{
student stu = new student();  //实例化一个student类型
int Chinese = stu.Chinese;   //出错了,internal 保护类型无法在这个项目中访问
int Math = stu.Math;  //出错,同上
int English = stu.English;  // 成功访问public保护类型

}
}
}


 我们在Class_internal中使用internal与public访问类型,在另一个项目Test_internal中只能访问到public访问类型。这就是internal

protected internal修饰符

                      Protected Internal 访问修饰符允许一个类将其成员变量和成员函数对同一应用程序内(项目)的子类以外的其他的类对象和函数进行隐藏。(不太明白) 换句话说就是同时拥有protected 与 internal的性质,protected访问限制在子类,能够在另一个程序集(项目)中通过继承方式访问到,internal是限制其他项目的访问,两个限制叠加,protected类型在另一个项目中不能通过继承的子类访问到。

 

 

 

      感谢您对HC666的支持^_^,欢迎提出宝贵意见
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息