public interface ITimeable {}
public class TimedDoor : ITimeable {}
public static class Timer
{
public static void Add(ITimeable obj)
{
Console.Write("Add with parameter - ITimeable");
}
public static void Add(TimedDoor obj)
{
Console.Write("Add with parameter - TimedDoor");
}
}
public class BaseClient<T> where T : ITimeable
{
public T TimedDoorObject;
public virtual void Init()
{
Timer.Add(TimedDoorObject);
}
}
public class Client : BaseClient<TimedDoor>
{
public Client()
{
TimedDoorObject = new TimedDoor();
}
public override void Init()
{
Timer.Add(TimedDoorObject);
}
}
在这个Client.Init()
回报"Add with parameter - TimedDoor"
但是如果 Client 没有覆盖 Init(),
public class Client : BaseClient<TimedDoor>
{
public Client()
{
TimedDoor = new TimedDoor();
}
}
在这里,Client.Init()
返回"Add with parameter - ITimeable"
这是怎么回事?TimedDoorObject
在运行时这两种情况下都是相同的。