class MainClass
{
public static void Main(string[] args)
{
string splits = new string('-', 20);
/*
* 1.重载:同一作用域-方法名相同、参数个数不同、参数类型不同、返回类型可以不同
*/
Console.WriteLine(splits);
MainClass main = new MainClass();
main.ShowInfo();//A
main.ShowInfo("string");//B
main.ShowInfo(1);//C
main.ShowInfo("string",1);//D
/*
* 2.重写:虚方法
*/
Console.WriteLine(splits);
Parent pc = new Child();
pc.Test();//虚方法,被子类重写了
/*
* 3.重写:抽象方法
*/
Console.WriteLine(splits);
Parent ps = new Son();
ps.Test2();//抽象方法,被子类实现具体方法
Console.ReadLine();
}
private void ShowInfo(){}
private void ShowInfo(string msg){}
private void ShowInfo(int level){}
private int ShowInfo(string msg,int level){return 0; }
}
abstract class Parent
{
public Parent()
{
Console.WriteLine("1.Parent");
//Console.WriteLine("2.{0}",this.GetType().Name);
//System.Reflection.MethodBase method = new System.Diagnostics.StackTrace().GetFrame(0).GetMethod();
//Console.WriteLine("3.ClassName:{0},MethodName:{1}", method.ReflectedType.Name, method.Name);
}
public virtual void Test()
{
Console.WriteLine("1.ClassName:Parent,MethodName:Test");
//Console.WriteLine("2.ClassName:{0},MethodName:{1}",this.GetType().Name,new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name);
//System.Reflection.MethodBase method = new System.Diagnostics.StackTrace().GetFrame(0).GetMethod();
//Console.WriteLine("3.ClassName:{0},MethodName:{1}", method.ReflectedType.Name, method.Name);
}
public abstract void Test2();
}
class Child:Parent
{
public Child()
{
Console.WriteLine("1.Child");
//Console.WriteLine("2.{0}",this.GetType().Name);
//System.Reflection.MethodBase method = new System.Diagnostics.StackTrace().GetFrame(0).GetMethod();
//Console.WriteLine("3.ClassName:{0},MethodName:{1}", method.ReflectedType.Name, method.Name);
}
public override void Test()//使用new关键字隐藏父类的Test,编译器不会报警
{
Console.WriteLine("1.ClassName:Child,MethodName:Test");
//Console.WriteLine("2.ClassName:{0},MethodName:{1}", this.GetType().Name, new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name);
//System.Reflection.MethodBase method = new System.Diagnostics.StackTrace().GetFrame(0).GetMethod();
//Console.WriteLine("3.ClassName:{0},MethodName:{1}", method.ReflectedType.Name, method.Name);
}
public override void Test2()
{
Console.WriteLine("1.ClassName:Child,MethodName:Test2");
}
}
class Son:Parent
{
public Son()
{
Console.WriteLine("2.{0}",this.GetType().Name);
}
public override void Test()
{
Console.WriteLine("1.ClassName:Son,MethodName:Test");
}
public override void Test2()
{
Console.WriteLine("1.ClassName:Son,MethodName:Test2");
}
}
/*
* A与B/C比较,参数个数不同;B与C比较,参数类型不同;A/B/C与D比较,参数个数、返回类型均不同;
*/