重载的定义:
在同一个类中,方法名称相同,但是参数列表不同(参数个数不同、参数类型不同、不同类型的排列顺序不同),称之为方法的重载,方法的重载不包含继承关系,重载不用考虑方法的返回值类型
static void Main(string[] args)
{
Tell();
Tell(5);
Tell("五");
Tell(5, "五");
Tell("5", 5);
Console.ReadLine();
}
static void Tell()
{
Console.WriteLine("Hellow World");
}
static void Tell(string a)
{
Console.WriteLine("字符串" + a);
}
static void Tell(int a)
{
Console.WriteLine("数字" + a);
}
static void Tell(string a,int b)
{
Console.WriteLine(a + b);
}
static int Tell(int a,string b)
{
Console.WriteLine("数字:" + a + "字符串:" + b);
return a;
}
结果显示:
重写的定义:
在父类中有一个方法,但是子类中也需要这个方法的名称,但是子类中的方法的逻辑与父类方法中的逻辑不相同,则需要在子类中对这个方法进行重写
class Father
{
public void Work()
{
Console.WriteLine("父类的工作是上班");
}
}
class Son:Father
{
public void Work()
{
Console.WriteLine("子类的工作是学习");
}
}
class Program
{
static void Main(string[] args)
{
Son s = new Son();
s.Work();
Console.ReadLine();
}
}
**输出结果:**
