1. Let the method return multiple parameters
1.1 results of defining variables stored in methods in Vitro
Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace Method
{
Class Program
{
Public static int quotient;
Public static int remainder;
Public static void Divide (int x, int y)
{
Quotient = x/y;
Remainder = x % y;
}
Static void Main (string [] args)
{
Program. Divide (6, 9 );
Console. WriteLine (Program. quotient );
Console. WriteLine (Program. remainder );
Console. ReadKey ();
}
}
}
1.2 use output and input parameters
Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace Method
{
Class Program
{
Public static void Divide (int x, int y, out int quotient, out int remainder)
{
Quotient = x/y;
Remainder = x % y;
}
Static void Main (string [] args)
{
Int quotient, remainder;
Divide (6, 9, out quotient, out remainder );
Console. WriteLine ("{0} {1}", quotient, remainder );
Console. ReadKey ();
}
}
}
2. Method Overloading
Method Overloading is an important extension of object-oriented programming features.
Methods that constitute heavy loads have the following features:
(1) Same method name
(2) The method parameter list is different.
There are three criteria for determining the second point above. If any of the criteria is met, the list of methods parameters may be different:
(1) The number of method parameters is different:
(2) The method has the same number of parameters, but the parameter types are different.
(3) methods have the same number of parameters and parameter types, but the order of parameter types is different,
Note that the type of the return value of a method is not a condition for determining method overloading.
3. Hide Methods
Copy codeThe Code is as follows: namespace method hiding
{
Class Program
{
Static void Main (string [] args)
{
Parent p = new Child ();
P. show ();
Console. ReadKey ();
}
}
Class Parent
{
Public void show ()
{
Console. Write ("parent class method ");
}
}
Class Child: Parent
{
Public new void show ()
{
Console. Write ("subclass method ");
}
}
}
Copy codeThe Code is as follows: namespace method hiding
{
Class Program
{
Static void Main (string [] args)
{
Parent. show ();
Console. ReadKey ();
Child. show (); // parent class Method
}
}
Class Parent
{
Public static void show ()
{
Console. Write ("parent class method ");
}
}
Class Child: Parent
{
Public static new void show ()
{
Console. Write ("subclass method ");
}
}
}
If the storage permission of a Member is not specified, all members are private.
Copy codeThe Code is as follows: namespace method hiding
{
Class Program
{
Static void Main (string [] args)
{
Parent p1 = new Parent ();
Parent p2 = new Child ();
P1.show (); // parent class Method
P2.show (); // parent class Method
(Child) p2). show (); // parent class Method
Console. ReadKey ();
}
}
Class Parent
{
Public void show ()
{
Console. WriteLine ("parent class method ");
}
}
Class Child: Parent
{
New void show ()
{
Console. WriteLine ("subclass method ");
}
}
}
4. Method rewriting and virtual method calling
Copy codeThe Code is as follows: namespace method Rewriting
{
Class Program
{
Static void Main (string [] args)
{
Parent p1 = new Parent ();
Parent p2 = new Child ();
P1.show ();
P2.show ();
(Parent) p2). show (); // subclass Method
Console. ReadKey ();
}
}
Class Parent
{
Public virtual void show ()
{
Console. WriteLine ("parent class method ");
}
}
Class Child: Parent
{
Public override void show ()
{
Console. WriteLine ("subclass method ");
}
}
}