我阅读了以下问题(我会以与给定答案相同的方式解决它):将派生类型作为参数传递给抽象类
但是为什么它无法value
从派生类中找到属性呢?即使我添加类型转换也是不可能的:
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)
{
// Here value is not found
return a.value + b.value;
}
public float value;
}
类型转换的示例也不起作用(并且显示了建议冗余类型转换):
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)
{
// Here value is not found even with type cast
return ((derived)a).value + ((derived)b).value;
}
public float value;
}