C#方法的重載

來源:互聯網
上載者:User

在前面的例子中,我們實際上已經看到了建構函式的重載。

程式清單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);    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.