在前面的例子中,我们实际上已经看到了构造函数的重载。
程序清单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); } }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索static
, s7-200
, student
, public
, console
, weight
, s
, s igit
, WriteLine
, void
, S方法
/s
c站、c语言、cf、ch、c罗,以便于您获取更多的相关知识。