110,533
社区成员
发帖
与我相关
我的任务
分享
class OverloadAndOverride
{
public static void Main()
{
int a = 5;
Child child = new Child();
child.Call(a);
Console.ReadKey();
}
}
public class Base
{
public virtual void Call(int input)
{
Console.WriteLine("This is Base[int]::Call(input={0})",input);
}
}
public class Child : Base
{
public override void Call(int input)
{
Console.WriteLine("This is Child[int]::Call(input={0})", input);
}
public void Call(double input)
{
Console.WriteLine("This is Child[double]::Call(input={0})", input);
}
}
IL_000c: callvirt instance void Demo1.Child::Call(float64)
#define INT
#define DOUBLE
using System;
using System.Reflection;
public class B
{
public B()
{
Console.WriteLine("调用基类开始:ClassB!");
int val = 100;
this.Func(val);
}
#if INT
public virtual void Func(int val)
{
Console.WriteLine("Type is {0},{1}", this.GetType(), "ClassB-Func!-int");
}
#endif
public virtual void Func(long val)
{
Console.WriteLine("Type is {0},{1}", this.GetType(), "ClassB-Func!-long");
}
}
public class BB : B
{
public BB()
{
Console.WriteLine("调用扩展类开始:ClassBB!");
int val = 100;
this.Func(val);
}
#if INTs
public override void Func(int val)
{
Console.WriteLine("Type is {0},{1}", this.GetType(), "ClassBB-Func!-int");
}
#endif
public override void Func(long val)
{
Console.WriteLine("Type is {0},{1}", this.GetType(), "ClassBB-Func!-long");
}
#if DOUBLE
public virtual void Func(double val)
{
Console.WriteLine("Type is {0},{1}", this.GetType(), "ClassBB-Func!-double");
}
#endif
}
public class BBB : BB
{
public BBB()
{
Console.WriteLine("调用扩展类开始:ClassBBB!");
int val = 100;
this.Func(val);
}
#if INTs
public override void Func(int val)
{
Console.WriteLine("Type is {0},{1}", this.GetType(), "ClassBBB-Func!-int");
}
#endif
public override void Func(long val)
{
Console.WriteLine("Type is {0},{1}", this.GetType(), "ClassBBB-Func!-long");
}
#if DOUBLE
public override void Func(double val)
{
Console.WriteLine("Type is {0},{1}", this.GetType(), "ClassBBB-Func!-double");
}
#endif
public void Func(string val)
{
Console.WriteLine("Type is {0},{1}", this.GetType(), "ClassBBB-Func!-string");
}
}
class Test
{
static void Main()
{
BB bb = new BBB();
int val = 100;
Console.WriteLine("变量BB类型开始:ClassBB!");
bb.Func(val);
//Reflection
Type[] types=new Type[3];
types[0] = Type.GetType("B");
types[1] = Type.GetType("BB");
types[2] = Type.GetType("BBB");
for( int i = 0 ; i<3;i++)
{
Console.WriteLine("======={0}=====",types[i].ToString());
MemberInfo[] memberInfos = types[i].GetMembers();
foreach (MemberInfo memberInfo in memberInfos)
{
Console.WriteLine("{0} is a {1},Name is {2}", memberInfo, memberInfo.MemberType, memberInfo.MetadataToken);
}
}
Console.ReadKey();
}
}
//[0]重写方法不被视为是在类上进行声明的,而是在基类上声明的方法的新实现。
//[1]非构造调用:派生类的原始方法相匹配的方法
//[2]非构造调用:派生类的原始方法相兼容的方法
//[3]非构造调用:基类的方法相匹配的方法
//[4]非构造调用:基类的方法相兼容的方法
//[5]构造调用:派生类的原始方法相匹配的方法
//[6]构造调用:派生类的原始方法相兼容的方法
//[7]构造调用:基类的方法相匹配的方法
//[8]构造调用:基类的方法相兼容的方法
//=======B=====
//Void Func(Int32) is a Method,Name is 12426544*A(B)
//Void Func(Int64) is a Method,Name is 12426552*(B)
//=======BB=====
//Void Func(Int64) is a Method,Name is 12426664+(B)
//Void Func(Double) is a Method,Name is 12426672*(BB)
//Void Func(Int32) is a Method,Name is 12426544A(B)
//=======BBB=====
//Void Func(Int64) is a Method,Name is 12426784+(B)
//Void Func(Double) is a Method,Name is 12426792+(BB)
//Void Func(Int32) is a Method,Name is 12426544A(B)
//=======B=====
//Void Func(Int32) is a Method,Name is 100663298A*
//Void Func(Int64) is a Method,Name is 100663299B*
//=======BB=====
//Void Func(Int64) is a Method,Name is 100663301B+
//Void Func(Double) is a Method,Name is 100663302C*
//Void Func(Int32) is a Method,Name is 100663298A
//=======BBB=====
//Void Func(Int64) is a Method,Name is 100663304B+
//Void Func(Double) is a Method,Name is 100663305C+
//Void Func(System.String) is a Method,Name is 100663306D*
//Void Func(Int32) is a Method,Name is 100663298A