Overload: in the same class, the method name is the same, and the parameter list is different. Different parameter lists include: the number of parameters is different, and the parameter type is different.
1 using system; 2 using system. collections. generic; 3 using system. text; 4 5 namespace overloading 6 {7 class Program 8 {9 public static int max (int I, Int J) // static method 10 {11 if (I> J) 12 return I; 13 else14 return J; 15} 16 public static double max (double I, Double J) // reload Static Method 17 {18 if (I> J) 19 return I; 20 else21 return J; 22} 23 static void main () 24 {25 console. writeline (max (1, 2); 26 console. writeline (max (1.1, 120.02); 27 console. readline (); 28} 29} 30}
Code Analysis:
In the preceding example, there are multiple Max methods, but because the parameter types in the methods are different, when the max method is called in a program, the system automatically searches for methods that match the corresponding parameters.
Run the preceding code. The output result is 4.3.
Methods With the same name are distinguished by the number, type, and order of parameters. Different methods can be defined as long as any of the three attributes is different. For example:
Int methodname (int I, Double J)
Int methodname (int I, Double J, int K)
Int methodname (int I, Int J)
Int methodname (double I, Int J)
Override: it refers to the inheritance of two classes. When a subclass overrides the method of the parent class, the subclass method overwrites the method of the parent class, that is, the method of the subclass is called. the method in the parent class must have the modifier virtual, and the method in the subclass must be well-known override. (The method name must be the same, and the parameters must be the same) rewrite format:
1 // 2 Public Virtual void method () 3 {4 5} 6 in the parent class // In the subclass: 7 public override void method () 8 {9 10}
After rewriting, the method () method is accessed by the parent class object and the subclass object, and the result is the method that is re-defined in the subclass. The method in the parent class is equivalent to the method that is overwritten by the slave class. in the subclass, we define different implementations of a method to meet our own needs.