我想知道是否可以在c#中实现与下面代码类似的签名:
public abstract class baseClass
{
public abstract float function(derivedType a, derivedType b);
}
public class derived: baseClass
{
public override float function(derived a, derived b)
{
return something to do with a and b;
}
}
希望我能清楚地知道我要做些什么,欢呼。
编辑:我尝试了以下但没有成功,希望它也可以澄清我尝试做的事情:
public abstract class baseClass
{
public abstract float function<T>(T a, T b) where T:baseClass;
}
public class derived: baseClass
{
public override float function<derived>(derived a, derived b)
{
return a.value + b.value;
}
public float value;
}
显然,派生的内容并未包含&#39; value&#39;的定义。据视觉工作室说。
编辑,解决方案:
public abstract class baseClass<T> where T: baseClass<T>
{
public abstract float function(T a, T b);
}
public class derived : baseClass<derived>
{
public override float function(derived a, derived b)
{
return a.value + b.value;
}
public float value;
}
感谢大家的帮助:)
答案 0 :(得分:3)
如果我理解你正在寻找什么,你所要做的就是:
public abstract class baseClass<T> where T : baseClass<T>
{
public abstract float Foo(T a, T b);
}
public class derived : baseClass<derived>
{
public override float Foo(derived a, derived b)
{
throw new NotImplementedException();
}
public float value;
}