通过继承更改返回签名 - 多态性

时间:2008-11-05 22:03:58

标签: c# .net polymorphism

只是想知道是否有办法做到以下几点:

public Interface IDataField 
{
    object GetValue();
}

public Interface IComplexDataField : IDataField
{
    object GetDefaultValue();
}

public class MyBase
{
    private IDataField _DataField;

    public MyBase()
    {
        this._DataField = this.CreateDataField();
    }

    public virtual IDataField CreateDataField()
    {
        return new DataField(); //Implements IDataField 
    }

    **public virtual IDataField GetDataField()**
    {
        return this._DataField;
    }

    public void SomeMethod()
    {
        this.GetDataField().GetValue();
    }
}


public class MyComplexBase : MyBase
{
    public override IDataField CreateDataField()
    {
        return new ComplexDataField(); //Implements IComplexDataField which Implements IDataField 
    }

    **public override IComplexDataField GetDataField()**
    {
        return (IComplexDataField)base.GetDataField();
    }

    public void SomeComplexSpecificMethod()
    {
        this.GetDataField().GetValue();
        this.GetDataField().GetDefaultValue();
    }
}

干杯 安东尼

3 个答案:

答案 0 :(得分:4)

你想要的是返回类型的协方差。这在C#中不存在。

你可以让MyBase通用:

public class MyBase<T> where T : IDataField
{
    public virtual T CreateDataField()
    {
        ... etc ...
    }
}

或者,您可以使用“new”重新声明该方法并隐藏基类方法。两者都没有特别吸引人......

答案 1 :(得分:1)

您在C#3.0中最接近的方法是使用单独的virtual方法(如CreateDataField方法)执行实际工作并可以覆盖,然后使用new重新声明公共方法。即在基地:

public IDataField GetDataField()
{
    return GetDataFieldImpl();
}
protected virtual IDataField GetDataFieldImpl()
{
    // return a basic version
}

并在子类中:

protected override IDataField GetDataFieldImpl()
{
    // do something more fun
}
public new IComplexDataField GetDataField()
{
    return (IComplexDataField)GetDataFieldImpl();
}

答案 2 :(得分:0)

您尝试做的事情称为Covariant返回类型。它们不存在于当前版本的C#中,但有人说它是为下一个版本引入的。

此处说明了您的最佳方法:covariant return types

public class MyBase<T> where T : IDataField, new()
{
    public virtual T CreateDataField()
    {
        return new T();
    }

    public virtual T GetDataField()
    {
        return this._DataField;
    }

}

public class MyComplexBase : MyBase<ComplexDataField>
{
   ...
}