C # basic parsing V (encapsulation)

Source: Internet
Author: User

Hello, everyone. Today, I will continue to discuss with you one of the object-oriented features in C # language-encapsulation.

In C #, object-oriented programming has three features: encapsulation, inheritance, and polymorphism. First, let's take a simple look at object-oriented. The so-called object-oriented is the skills and concepts in application development. It is mainly a design idea, or OOP Technology for short, the object-oriented programming language can make the complex work organization clearer and easier to write. It also has reusability, flexibility and scalability. However, to tell the truth, if the object orientation is really unclear, it is like asking me what mathematics is.

No. 1 the core of object-oriented systems is classes and objects. A class is a set of objects with the same attributes and behaviors. For example, the director is a class, the actors are a class, and the audience is also a class. They all have the same attributes and behaviors. They are abstract classes and all refer to a group of people. The so-called object is that everything is an object. For example, a computer, a mobile phone, an apple, and so on are all objects. An object describes the characteristics of an object and abstracts an actual object or method.

To deepen my impression, I drew a picture for you:



 

The object has four access modifiers: public permission, internal assembly protection permission, protected protection permission, and private permission. The class includes three members: field, attribute, and method. Fields are the information data of the storage class, and most fields are private. The attribute is to protect private fields and is public. The attribute includes two accessors: the read-only get accessors and the write-only set accessors. The method is the action of the class, that is, what the class is going to do.
No. 2 briefly understands object-oriented. Next let's take a look at the definition of a method. The so-called method is behavior, that is, what to do, and there is no specific definition of the method, generally, it refers to a method or behavior that is used to obtain something for a certain purpose.
Here is an example of a simple method call:
Creation class:
1 class Regisseur // actor class
2 {
3 string name;
4 public string Name
5 {
6 get {return name ;}
7 set {name = value ;}
8}
9 // method for selecting a movie name. The movieName parameter is the movie name.
10 public void SelectMovieName (string movieName)
11 {
12 Console. WriteLine ("Director:" + name + "shooting" + movieName + "Wonderful Movie ");
13}
14}

Call the class method in the Main method:
1 class Program
2 {
3 static void Main (string [] args)
4 {
5 Regisseur r = new Regisseur (); // instantiate the object
6 r. Name = "Xu Ke"; // call attribute assignment
7 r. SelectMovieName ("Longmen flying Armor"); // call the method to select the movie name
8 Console. ReadKey ();
9}
10}

Running result:




 

Let's take a look at the syntax for defining methods of classes:
Access modifier return type method name (parameter list)
{
Method subject;
}

Let me briefly explain that the returned type is the data type, indicating that a value can be obtained after this method is called. If no value is returned, this type is void,
For example, private string Qingpingguo (string n) returns the string return value with the keyword "return ".
Public void Qingpingguo (string n) does not return any value
The method name is to give a name to the method. Note that the first letter must be capitalized when the name is obtained.
The parameter list allows you to pass data to a method when calling a method. The method produces different results based on different parameters.
Not as good as: the private string Qingpingguo (string n) parameter is a string type
Private void Qingpingguo (int I, string n) parameters: int and string
The main body of the method is the function of this method.
No. 3 in the definition of a method, there is a method called static method. A static method can be called directly without instantiating an object. The syntax is as follows:
Access modifier static return type method name (parameter list)
{
Method subject
}

Static methods are simpler than common methods, especially when calling them. Although the results are the same, I do not recommend static methods. Why? Because the static attributes and Methods Modified by the static keyword are created when the program starts to run and disappear until the program ends, it wastes a lot of resources, so I hope you will not use static methods unless necessary.
Reload of Method No. 4
Method overloading means that the method name is the same, the method parameter type is different, or the number of parameters is different.
Example:
1 class Regisseur
2 {
3 public void Qingpingguo (string title, string player) // Two Parameters
4 {
5 Console. WriteLine ("movie name:" + title );
6 Console. WriteLine ("Starring:" + player );
7}
8 public void Qingpingguo (string title, string player, string player1) // three parameters
9 {
10 Console. WriteLine ("movie name:" + title );
11 Console. WriteLine ("Starring:" + player + "and" + player1 );
12}
13}

Call the overload method in the Main method
1 static void Main (string [] args)
2 {
3 Regisseur r = new Regisseur (); // instantiate the object
4 r. Qingpingguo ("Longmen flying Armor", "Jet Li"); // call the methods of the two parameters
5 r. Qingpingguo ("Longmen flying Armor", "Jet Li", "Zhou Xun"); // reload the method and call the methods of the three parameters
6 Console. ReadKey ();
7}
8

Running result:




 

The overload of this method is relatively simple, so I will not explain it too much. Note that different return types of methods are not called overload.
No. 5 Constructor
The function of constructor is to assign values to attributes when instantiating an object, which simplifies our code. The special feature of constructor is that the method name is the same as the class name and there is no return type, in addition, the constructor generally uses the public access modifier. If there is no defined constructor in the class, a constructor without parameters will be automatically created in the compiler. This constructor is like an implicit constructor, the method we write is called an explicit constructor. In an implicit constructor, the compiler automatically assigns values to fields. The constructor method is relatively simple, so we will not go into it.
No. 6 parameter transfer
Parameter transfer is divided into value transfer and reference transfer. For value transfer, I think you will understand the literal meaning. So let's just escape to the reference transfer!
When passing a reference, we should note that when defining a method with the keyword "ref", we must add the keyword ref before referencing the passed parameter, for example, static string qingpingguo (ref string name) {} must be assigned a value before passing the parameter. Let's take a look:
1 class Program
2 {
3 static void Main (string [] args)
4 {
5 string n = "";
6 string m = qingpingguo (ref n); // call method value transfer
7 Console. WriteLine (m); // return value of the output
8 Console. WriteLine (" +" + n); // output parameter value
9 Console. ReadKey ();
10}
11 static string qingpingguo (ref string name)
12 {
13 name = "qingapple ";
14 Console. WriteLine ("I like qingapple ");
15 return "return ";
16}
17}

Running result:




 

Here, we should note that the value passes the copy of the parameter, not itself; the reference passes the parameter itself. In the keyword ref we just mentioned, another one corresponds to the out keyword. when the control is passed back to the call method, any changes made to parameters in the method are reflected in the variable. When you want the method to return multiple values, it is very useful to declare the out method. You can still return a value using the out parameter. A method can have more than one out parameter. To use the out parameter, you must use the parameter as the out parameter to pass it to the method explicitly. Simply put, the difference between them is that ref requires parameter initialization before being passed to the function, and out is not required. out is initialized in the method, which is common in Platform calls. Out is applicable when multiple return values are needed, while ref is used when the method to be called modifies the reference of the caller.

Summary: 1. object-oriented classes and methods;

2. method definition;

3. static method;

4. Method Overloading;

5. constructor;

6. parameter transfer;

The end!

This article is my personal opinion. If there are any imperfections or inaccuracies, you are welcome to criticize them.
 

From apple

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.