在前面的例子中,我們實際上已經看到了構造函數的重載。
程序清單11-7:
using System; class Vehicle //定義汽車類 { public int wheels; //公有成員:輪子個數 protected float 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); } };
類的成員方法的重載也是類似的。類中兩個以上的方法(包括隱藏的繼承而來的方法),取的名字相同,只要使用的參數類型或者參數個數不同,編譯器便知道在何種情況下應該調用哪個方法,這就叫做方法的系列重載。
其實,我們非常熟悉的Console類之所以能夠實現對字符串進行格式化的功能,就是因為它定義了多個重載的成員方法:
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);
我們舉例子來說明重載的使用。學生類中包含有學生姓名、性別、年齡、體重等信息。我們需要比較學生之間的年齡和體重,我們可以采用下面的代碼。
程序清單11-8:
using System; class Student //定義學生類 { 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}'s age is bigger than {1}'s",s1.s_name,s2.s_name); else Console.WriteLine("{0}'s age 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); } }
由於C#支持重載,我們可以給兩個比較大小的方法取同一個名字max,程序員在調用方法時,只需要在其中帶入實參,編譯器就會根據實參類型來決定到底調用哪個重載方法。程序員只需要使用max這個方法名,剩下的就是系統的事了。那樣,代碼我們改寫為:
程序清單11-9:
using System; class Student //定義學生類 { 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 age is bigger than {1}'s",s1.name,s2.s_name); else Console.WriteLine("{0}'s age 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); } }