Encapsulation:
is defined as "enclosing one or more projects in a physical or logical package." In object-oriented programming methodology, encapsulation is used to prevent access to implementation details. That is to say, the implementation details are wrapped up, so that the very complex logic can be conveniently used by others after being packaged. Others do not need to understand how it is implemented, and they can get the desired results by simply passing in the required parameters. Encapsulation is implemented using access modifiers. An access modifier defines the scope and visibility of a class member.
Access modifiers:
In Class Class Declaration and Definition, I briefly introduced the access modifiers. It is not very specific. Here we learn about the access modifiers in depth. ;
that access modifier in C# have 5 types: public, private, protected, internal, protected internal; Its member variables and member functions are exposed to other functions and objects. Any public member can be accessed by outside classes. (Access to members is not restricted, subclasses are allowed, and objects are also allowed)
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()); } } }
Output results:
In the above example, all in student The members are all public type access, so his instantiated object stu can access all members.
private modifier
The Private access modifier allows a class to hide its member variables and member methods from its objects. Only methods within the same class can access its private members. Even an instance of a class cannot access its private members (no access by its subclasses or objects is allowed).
We will use the above example:
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 方法 } } }
Since there are private access restrictions on the variables Chinese and Math method Total in the student class, they cannot be accessed through the object stu
protected modifier
The Protected access modifier allows a subclass to access the member variables and member functions of its base class. This helps implement inheritance. (Object access is not allowed, method access in the class is allowed, and subclass access is allowed)
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 modifier
The Internal access modifier allows a class to expose its member variables and member methods to Other methods and objects in the current assembly (current project). In other words, any member with the internal access modifier can be accessed by any class or method defined within the assembly (project) in which the member is defined.
To learn the internal modifier, we must first learn to use Vs2010 to create multiple projects and how to reference projects:
First create a project file: name it Test_internal
After clicking OK, find the solution manager:Right-click "Reference"--->Add Reference--->Project. Find the project name to be referenced in the project name list. Here we select Class_internal. Click OK. Class_internal will appear in the reference list. The reference is successful.
We open the program in Class_internal .cs edit the following codeusing 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) { } } }
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保护类型 } } }
We use internal and public access types in Class_internal, and only public access is available in another project Test_internal type. This is internal
protected internal modifier
The Protected Internal access modifier allows a class to hide its member variables and member functions from other class objects and functions other than subclasses within the same application (project). (I don’t quite understand) In other words, it has the properties of protected and internal at the same time. Protected access is restricted to subclasses and can be accessed through inheritance in another assembly (project). Internal restricts access to other projects. The two Restrictions on overlay, protected types cannot be accessed in another project through inherited subclasses.
The above is the content of C# Learning Diary 21----Encapsulation and access modifiers. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!