C # language series lecture (6) method

xiaoxiao2021-03-06  78

● Method is also known as member functions, which reflects the behavior of classes or objects. ● The method is divided into static methods and examples. The static method can only operate static domains, while the example method can operate both static domains or operated by the example domain. ● Methods There are five types of access modifiers as domain --public, protected, internal, protected in both, private. Method parameter method is also known as member function (Member function). The parameters of the method have four types: BY value, by reference, output parameter (by output), array parameters (by array). The transmission value parameter does not require additional modifiers, the address parameter requires the modifier REF, the output parameter requires modifier OUT, and the array parameters require modifier params. The pass value parameter is changed in the method call, and the parameter of the incoming method is not changed after the method call is completed, but the value of the original incoming is retained. The inquiry of the inline parameter is the opposite. If the method calling process changes the value of the parameter, the parameter of the incoming method is also changed after the call is completed. In fact, it can clearly see a copy of the meaning of both the name-the transmission value parameter is a copy of the calling parameter, and the address parameter passes the memory address of the calling parameter, which is pointed out inside and outside the method. The same storage location. Note that the method calls for the service parameters should be plus the Ref modifier when they are declared or called. In the case of passing values ​​and delivery, the C # enforcement requirements parameters are explicitly initialized by the user before passing, otherwise the compiler is wrong! But if there is a function that does not rely on the first value of the parameter, what should I do when the function is required to get its value (often in the function return value more than one specifically, this skill is required)? The answer is used by OUT modified output parameters. However, it is necessary to note that the output parameter has a certain difference between the return value of the usual function: the function return value often exists in the stack, pops up when it returns; and the output parameter requires the user to pre-specify the storage location, that is, the user needs to declare the variable in advance - of course also Can be initialized. All output parameters in the function must be assigned, otherwise the compiler error! OUT modifiers should also be used in function declarations and calls. In addition to acting as a special function of returning, the OUT modifier is similar to the REF modifier: an address. It can be seen that C # completely abandoned the traditional C / C language to give the programmer's great freedom, after all, C # is the next generation of efficient network platforms used to develop, security - including system security (system structure design) and Engineering Security (Avoiding the error of programmers) is an important consideration when it is design, of course, C # does not lose how many languages ​​are lost because of security, this is the excellence of C #! Array parameters are used to pass a large number of array set parameters. Array parameters can be arrays (eg: var), or may implicit into arguments, such as: 10, 20, 30, 40, 50. This provides a high scalability for the program. The difference between the same name method parameters will result in a polymorphic phenomenon, which is also a overloading method. It is to be pointed out that the compiler is bound to the method and method call when compiling. You can only overload the method by different parameters, other different (such as return values) cannot provide valid overload information for the compiler. Methods Inherited C # method Introduces four modifiers of Virtual, Override, Sealed, and Abstract to provide different inheritance needs. The virtual method of the class is to change the method of its implementation in the inheritance class of such, of course, this change is limited to the change in the method, rather than the change of the method head (method declaration).

The false method of the quilt change must be represented in the method header. When an imaginary method is called, the instance of such a class is also the runtime type (run-time type) of the object to determine which method is called. Look at the following example: use system; class parent {public void f () {console.writeline ("public Virtual void g () {console.writeline (" parent.g ");}} Child: parent {new public void f () {console.writeline ("child.f");} public override void g () {console.writeline ("child.g");}} Class test {static void main ) {Child b = new child (); parent a = b; AF (); bf (); Ag (); bg ();}} program is compiled, output: Parent.f child.f child.g Child.g You can see the declaration of the f () method in class Child takes overwriting (NEW) to shield the declaration of non-virtual methods f () in the parent class, and the g () method is override) Method of polymorphism. It should be noted that the NEW method and override are different, and from essentially rewriting method is compiled, and the overlay method is binding when running. It is worth pointing out that the virtual method can not be a static method - that is, it is not possible to use Static and Virtual to modify a method, which is determined by its runtime analysis mechanism. Override must be used in conjunction with Virtual, of course, will not be used at the same time as STATIC. What should I do if I don't want to make an imaginary method in a class's inheritance system? The answer is Sealed Override, which is modified with Sealed and Override to reach this purpose: Sealed Override public void f (). Note that this must be used in Sealed and Override, and it must also be a seal to cover a virtual method, or an imaginary method that is covered (rather than sealed). Sealing a non-virtual method is meaningless, it is also wrong. Abstract approach is logically similar to the false method, but cannot be called like virtual methods, is a declaration of an interface rather than implementation. Abstract methods have no way to achieve, nor allows you to do. The abstract method can also be static. Class containing abstract methods must be an abstract class, and must also add Abstract to modifiers. But abstract classes do not have to contain abstract methods. Subcorts in which an abstract class containing an abstract method must be covered and implemented (directly using Override). Or combination uses Abstract Override to continue abstraction, or not to provide any coverage and implementation, the latter behavior is the same.

转载请注明原文地址:https://www.9cbs.com/read-121282.html

New Post(0)