using System;
namespace ShouldCode
{
public interface IShouldBaseNotInterface
{
bool Show();
}
public class A1 : IShouldBaseNotInterface
{
public bool Show()
{
throw new NotImplementedException();
}
}
//A2:IShouldClassNotInterface
//A3:IShouldClassNotInterface
//...
public class A100000 : IShouldBaseNotInterface
{
public bool Show()
{
throw new NotImplementedException();
}
}
public abstract class Base : IShouldBaseNotInterface
{
public bool Show()
{
//第一次通过某个接口调用方法时会去查找具体类型,内部会有 Monomorphic Stub 优化。
Console.WriteLine("InterFace.Do 需要查找类型,运行时子类类型太多就会影响程序性能。");
Console.WriteLine("Base.Do 通过基类来减少查找的类型数量");
return true;
}
}
public class ShouldBaseNotInterface : Base
{
public Base Base { get; set; } = new ShouldBaseNotInterface();
public IShouldBaseNotInterface Interface { get; set; } = new ShouldBaseNotInterface();
public void ShouldClass()
{
System.Console.WriteLine(Base.Show());
}
public void NotInterFace()
{
System.Console.WriteLine(Interface.Show());
}
}
}