在C#,什么叫做方法重载,怎样实现方法重载,

来源:百度知道 编辑:UC知道 时间:2024/09/20 11:47:27
谁能给我一段代码,并解释一下

重载是指方法名称相同, 只是参数个数或类型不同. 方法功能是相同的.

除了重载, 还要了解以下几个关键字的用法,与区别:

virtual 定义为虚方法
override 对虚方法实现,具有多态性
new 把基类中的同名方法替换掉, 不具有多态性

//举个例子

//不使用方法重载

public string GetString(string strValues)
{
if(strValues.Length==0)
{
return "今天我遇到无名了。。。。";
}
return "Hello,"+strValues+"兄";
}

//使用重载就是

public string GetString()
{
return GetString("我是无名。。。你是?");
}
public string GetString(string strValues)
{
return strValues;
}

using System;
public class Test{
private string name;
private string sex;
public Test(){}
public Test(string name){
this.name=name;
Console.Write(name);
}
public Test(string name,string sex){
this.name=name;
this.sex=sex;
Console.Write(name+":"+sex);
}
public static void Main(){
Test t=new Test("