In the previous example, we have actually seen the overload of the constructor.
Program Listing 11-7:
Using System;
Class Vehicle//Definition of auto class
{
public int wheels;///Publicly owned member: number of wheels
protected float weight;//Protect Members: Weight public
Vehicle (){;}
Public Vehicle (int w,float g) {
wheels=w;
weight=g;
}
public void Show () {
Console.WriteLine ("The Wheel of Vehicle is:{0}", wheels);
Console.WriteLine ("The Weight of vehicle is:{0}", weight);
}
;
The overloads of the member methods of the class are similar. A class of more than two methods (including hidden inherited methods) with the same name, as long as the parameter type or number of parameters is used, the compiler knows under what circumstances it should call which method, which is called the method's series overload.
In fact, the console class that we are very familiar with is capable of formatting a string because it defines multiple overloaded member methods:
public static void WriteLine ();
public static void WriteLine (int);
public static void WriteLine (float);
public static void WriteLine (long);
public static void WriteLine (UINT);
public static void WriteLine (char);
public static void WriteLine (BOOL);
public static void WriteLine (double);
public static void WriteLine (char[]);
public static void WriteLine (string);
public static void WriteLine (Object);
public static void WriteLine (ULONG);
public static void WriteLine (string,object[]);
public static void WriteLine (String,object);
public static void WriteLine (Char[],int,int);
public static void WriteLine (String,object,object);
public static void WriteLine (String,object,object,object);
We give an example to illustrate the use of overloading. The student's class contains information about the student's name, gender, age, and weight. We need to compare the age and weight between students, we can use the following code.
Program Listing 11-8:
Using System;
Class Student//define Student class {public string s_name;
public int s_age;
public float s_weight;
Public Student (String N,int a,float W) {s_name=n;
S_age=a;
S_weight=w;
public int max_age (int x,int y) {if (x>y) return x;
else return y;
public float max_weight (float x,float y) {if (x>y) return x;
else return y;
Class Test {public static void Main () {Student s1=new Student ("Mike", 21,70);
Student s2=new Student ("John", 21,70); if (S1.max_age (s1.s_age,s2.s_age) ==s1.s_age) Console.WriteLine (' {0} ' is bigger than {1} ' s '), S1.s_name,s2.s_na
ME);
else Console.WriteLine (' {0} ' is smaller than {1} ' s ', s1.s_name,s2.s_name); if (S1.max_weight (s1.s_weight,s2.s_weight) ==s1.s_weight) Console.writelie ("{0} ' s weight is bigger than {1} ' s", S1.)
S_name,s2.s_name); else Console.WriteLine ("{0} ' s weight is smaller than {1} ' s ", s1.s_name,s2.s_name); }
}
Because C # supports overloading, we can give the same name to the two size methods Max, when the programmer calls the method, just bring the argument in, and the compiler will decide which overload method to invoke based on the argument type. The programmer only needs to use Max as the method name, and the rest is the system. So, the code we rewrite as:
Program Listing 11-9:
Using System;
Class Student//define Student class {public string s_name;
public int s_age;
public float s_weight;
Public Student (String N,int a,float W) {s_name=n;
S_age=a;
S_weight=w;
public int max (int x,int y) {if (x>y) return x;
else return y;
public float max (float x,float y) {if (x>y) return x;
else return y;
Class Test {public static void Main () {Student s1=new Student ("Mike", 21,70);
Student s2=new Student ("John", 21,70);
if (S1.max (s1.s_age,s2.age) ==s1.s_age) Console.WriteLine ("{0} ' s is bigger than {1} ', s1.name,s2.s_name);"
else Console.WriteLine (' {0} ' is smaller than {1} ' s ', s1.s_name,s2.s_name); if (S1.max (s1.s_weight,s2.s_weight) ==s1.s_weight) Console.WriteLine (' {0} ' s weight is bigger than {1} ' s '), S1.s_name
, s2.s_name); else Console.WriteLine ("{0} ' s weight is smaller than {1} ' s", s1.s_name,s2.)S_name); }
}