问题描述
|
public interface IAutomatizableEvent
{
Event AutomatizableEventItem { get; }
bool CanBeAutomatic { get; }
bool IsAutomaticallyRunning { get; }
bool OnBeforeAutomaticCall();
bool OnAutomaticCheck();
void OnAutomaticStart();
void OnAutomaticCancel();
}
public abstract class AutomatizableEvent : IAutomatizableEvent
{
public AutomatizableEvent()
{
}
#region Implementation of IAutomatizableEvent
public abstract Event AutomatizableEventItem { get; }
public abstract bool CanBeAutomatic { get; }
public abstract bool IsAutomaticallyRunning { get; }
public abstract bool OnBeforeAutomaticCall();
public abstract bool OnAutomaticCheck();
public abstract void OnAutomaticStart();
public abstract void OnAutomaticCancel();
#endregion
}
public static class EventSystem
{
public static List<EventSystemBase<Event,AutomatizableEvent>> AutomatizableEvents { get; set; }
[...]
}
\“类型\'AutomatizableEvent \'必须具有公共的无参数构造函数,才能在通用类\'EventSystemBase \'\中将其用作参数\'K \'
public abstract class EventSystemBase<T,K> : AutomatizableEvent
where T : Event
where K : AutomatizableEvent,new()
我的问题是... AutomatizableEvent没有公共无参数构造函数吗?
解决方法
错误描述是错误的,但是错误是正确的。
由于约束
where K : new()
,2ѭ不能用作通用参数K
。抽象类不满足该约束。
始终有效地保护抽象类的构造函数,因为只能将抽象创建为派生类的基础子对象,因此构造函数同样只能由派生类的构造函数调用,并且只能在构造函数链接中调用。具体地说,EventSystemBase<T,K>
在上下文new K()
中不能使用它。
,你不能实例化一个抽象类