重载和重写
重载:方法名相同,参数个数可能相同,也可能不相同,但是类型不同,返回值类型不同;
例如:public static int M(int n1,int n2){}
public static int M(int n1,int n2,int n3){}
public static string M(string n1,string n2){}
public static double M(double n1,double n2){}
重写:子类继承父类后对父类里面的虚方法重新实现;
static void Main(string[] args)
{
Teacher t = new Teacher;
t.Msg();
Console.ReadKey();
}
父类:
public class Person
{
public virtual void Msg()
{
Console.WriteLine("我是程序员");
}
}
子类:
public class Teacher : Person
{
public override void Msg()
{
Console.WriteLine("我是老师");
}
}
输出: 我是老师