C#方法的重载

在前面的例子中,我们实际上已经看到了构造函数的重载。

程序清单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罗,以便于您获取更多的相关知识。

时间: 2024-08-22 14:46:19

C#方法的重载的相关文章

在派生类中对虚方法进行重载

先让我们回顾一下普通的方法重载.普通的方法重载指的是:类中两个以上的方法(包括隐藏的继承而来的方法),取的名字相同,只要使用的参数类型或者参数个数不同,编译器便知道在何种情况下应该调用哪个方法. 而对基类虚方法的重载是函数重载的另一种特殊形式.在派生类中重新定义此虚函数时,要求的是方法名称.返回值类型.参数表中的参数个数.类型.顺序都必须与基类中的虚函数完全一致.在派生类中声明对虚方法的重载,要求在声明中加上override关键字,而且不能有new,static或virtual修饰符. 还是让我

ASP.NET方法如何重载的必备条件

 很多新手朋友们都不知道ASP.NET方法如何重载,下面为大家介绍下在设计重载方法的时候应该注意一下事项,感兴趣的朋友不要错过 决定方法是否构成重载有以下几个条件:    ◆ 在同一个类中:    ◆ 方法名相同:    ◆ 参数列表不同.    在设计重载方法的时候应该注意一下事项    1 避免随意更改重载中的参数名称.如果某个重载的一个参数与另一个重载的一个参数表示相同的输入,则这两个参数应具有同样的名称.    例如,不要执行下面的操作:  代码如下: public void Write

php函数重载的替代方法--伪重载详解_php实例

函数重载的替代方法-伪重载,下面看一个具体的实例代码. <? php //函数重载的替代方法-伪重载 // //确实,在PHP中没有函数重载这个概念,让很多时候我们无法进行一些处理,甚至有时候不得不在函数后面定义好N个参数 //在看到了func_get_arg,func_get_args,func_num_args,这三个函数的时候,你们是不是想起了什么? function testOne ( $a ) { echo (' 一个参数就这样 '); } function testTwo ( $a

java equals 方法的重载和复写

问题描述 java equals 方法的重载和复写 public class Person { private String name; public Person(String name){ this.name = name; } public String getName() { return name; } public boolean equals(Object other) { Person person = (Person)other; System.out.println("执行复

通信-BeginReceive方法没有重载3个参数代码如何修改

问题描述 BeginReceive方法没有重载3个参数代码如何修改 c#socket异步通信BeginReceive方法没有重载3个参数代码如何修改 public string SocketReceive() { string result = ""; try { MemoryStream stream = new MemoryStream(); Byte[] bytesReceived = new Byte[256]; int bytes = 0; do { bytes = clie

Java的this关键字的使用与方法的重载相关知识_java

Java this关键字详解 this 关键字用来表示当前对象本身,或当前类的一个实例,通过 this 可以调用本对象的所有方法和属性.例如: public class Demo{ public int x = 10; public int y = 15; public void sum(){ // 通过 this 点取成员变量 int z = this.x + this.y; System.out.println("x + y = " + z); } public static vo

Java面向对象基础---名词概念的理解:方法的重载、构造函数、封装性

方法的重载:方法名称相同,但参数的类型和个数不同,通过传递参数的个数及类型不同以完成不同功能的方法调用. 例如:System.out.println();属于方法的重载. 方法的重载一定是根据参数类型和个数来判断的. 构造函数:构建创造对象时调用的函数.作用:可以给对象进行初始化.Tip:函数名与类名相同,不用定义返回值类型,没有具体的返回值.   面向对象特征:1.封装(encapsulation)对外部不可见,保护程序的某些内同.2.继承性(inheritance)扩展功能.3.多态(pol

浅谈Java方法的重载_java

方法的重载是指一个类中可以定义有相同的名字,但参数不同的多个方法,调用时,会根据不用的参数表选择对应的方法 比如 public class Test { void max(int a,int b) { System.out.println(a>b ? a:b); } void max(double a,double b) { System.out.println(a>b ? a:b); } public static void main(String[] args) { Test t = ne

《.NET程序员面试秘笈》----面试题8 方法的重载和override有什么区别

面试题8 方法的重载和override有什么区别 .NET程序员面试秘笈[考点]对类体内函数的深刻理解,对重载机制的应用,对override的理解. [出现频率] [解答] 方法的重载和重写容易被混淆,重载是方法的名称相同,函数签名不同,进行多次重载以适应不同的需要.而重写(override)是进行基类中函数的扩展或改写,其签名必须与被重写函数保持一致. 本例通过多种不同形式的构造函数创建实例,并输出初始化的结果.在ch01目录下新建一个程序文件,并命名为Override.cs,编写代码如程序1

java必学必会之方法的重载(overload)_java

一.方法的重载 方法名一样,但参数不一样,这就是重载(overload). 所谓的参数不一样,主要有两点:第一是参数的个数不一样,第二是参数的类型不一样.只要这两方面有其中的一方面不一样就可以构成方法的重载了. package cn.galc.test; public class TestOverLoad { void max(int a, int b) { System.out.println(a > b ? a : b); } /* * int max(int a, int b) { * r