Hello, everyone. Today I will continue to discuss with you one of the object-oriented features in C # language ---Encapsulation
In C # language, object-oriented has three features:Encapsulation,Inheritance,Polymorphism. First, let's take a simple look at object-oriented. The so-called object-oriented is the application.ProgramThe skills and concepts in development are mainly a design concept, called OOP Technology for short.Programming LanguageIt makes 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 // Actors
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 + " Movie highlights " );
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 object
6 R. Name = " Xu Ke " ; // Call attribute assignment
7 R. selectmoviename ( " Longmen flying armor " ); // Call the method to select a 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 give you a brief explanation.Return typeIt 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
Method NameIt is to give a name to the method. Note that the first letter must be capitalized when the name is obtained.
Parameter ListWhen calling a method, you can pass data to the method through parameters. 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
Method subjectIs the function of this method.
No. 3 in the definition of a method, there is a method calledStatic MethodThe static method can be called directly without instantiating an object. The syntax is as follows:
Access ModifierStaticReturn 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.
No. 4Method Overloading
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 object
4 R. qingpingguo ( " Longmen flying armor " , " Jet Li " ); // Call Two Parameters
5 R. qingpingguo ( " Longmen flying armor " , " Jet Li " , " Zhou Xun " ); // Overload method, call the methods of 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. 5Constructor
The role of constructor is to assign values to attributes when instantiating objects, which can simplify ourCodeThe method name is the same as the class name, and no return type is returned. 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. 6Parameter 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 ); // Output Return Value
8 Console. writeline ( " There are " + N ); // Output parameter value
9 Console. readkey ();
10 }
11 Static String Qingpingguo ( Ref String Name)
12 {
13 Name = " Green Apple " ;
14 Console. writeline ( " I like green apple " );
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.
|
Author: green apple Motto: constantly reflect on yourself! Then change it! Technologies of interest:. net, database, JavaScript, C #, Ajax, winform, jquery, extjs Source: http://www.cnblogs.com/xinchun/ The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent andArticleThe original text connection is clearly displayed on the page. Otherwise, the legal liability is retained. |